Bonjoir,
je reviens pour un autre problème avec Spring et hibernate ( pour changer) que je vais tenter de vous exposer au mieux possible.
J'essaie de réaliser un système de login personnalisé avec Spring et hibernate (sans utiliser Spring Security, je précise).
Tout se passe bien niveau vue avec ce que j'ai réalisé jusqu'a ce que, lorsque je soumets mon formulaire de Login, PAF un NullPointerException surgit dans mon "LoginValidator"!
Il semblerait que le "usermanager" que j'utilise n'est pas injecté dans mon validator. J'aimerais donc savoir si quelqu'un aurait une idée du pourquoi du parceque.
Ci dessous, le code de mon LoginValidator :
Ci après le code de mon applicationcontext.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class LoginValidator implements Validator { protected final Log logger = LogFactory.getLog(getClass()); private UserManager userManager; public UserManager getUserManager() { return userManager; } public void setUserManager(UserManager userManager) { this.userManager = userManager; } @Override public boolean supports(Class clazz) { return Login.class.equals(clazz); } @Override public void validate(Object obj, Errors errors) { Login login = (Login) obj; if (login.getLogin() == null) { errors.rejectValue("login", "error.login.not-specified", null, "Value required."); } else { if (login.getPassword() == null) { errors .rejectValue("password", "error.password.not-specified", null, "Value required."); } else{ logger.info("Validating user " + login.getLogin()+" with password "+login.getPassword()); if (!userManager.checkLogin(login.getLogin(), login.getPassword())) { errors.reject("login","error.login.badcreds"); } } } } }
ci dessous, une partie de mon testlogin-serviette.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- General --> <bean id="userDao" class="testlogin.dao.UserDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>hibernate.cfg.xml</value> </property> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED, readOnly</prop> </props> </property> </bean> <bean id="userManagerTarget" class="testlogin.service.UserManagerImpl"> <property name="userDao"> <ref bean="userDao" /> </property> </bean> <bean id="userManager" parent="transactionProxy"> <property name="target"> <ref bean="userManagerTarget"/> </property> <property name="transactionAttributeSource"> <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/> </property> </bean> </beans>
Je me suis servi pour "apprendre" et tenter de mettre en pratique Hibernate et Spring, du tuto de Baptiste Meurant que l'on retrouve ici (que je remercie au passage pour partager ses connaissances!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <bean name="/login" class="testlogin.web.LoginFormController"> <property name="sessionForm" value="true" /> <property name="commandName" value="login" /> <property name="commandClass" value="testlogin.service.Login" /> <property name="validator"> <bean class="testlogin.service.LoginValidator" /> </property> <property name="formView" value="login" /> <property name="successView" value="hello" /> <property name="userManager" ref="userManager" /> </bean>) mais sans utiliser tapestry mais spring-webmvc à la place.
Il me semblait que en indiquant dans le testlogin-serviette.xml, dans les propriétés du bean "/login", le "<property name="userManager" ref="userManager" />" , le framework ferait l'injection de bean avec le même nom dans mon LoginValidator, mais ce n'est apparament pas le cas.
Si quelqu'un peut m'aider ou m'indiquer où je me suis planté, je lui en serai reconnaissant!merci beaucoup!
Partager