Voila mon problème, j'ai un formulaire qui demande un userId, et 2 date.
Le userid est obligatoire, et les date non, mais si elle ne sont pas nulle, alors elle doivent etre dans un format bien precis.
Le validateur vaut cela :
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 public class SelectUserValidator implements Validator { public boolean supports(Class aClass) { return SelectUserCommand.class.equals(aClass); } public void validate(Object object, Errors errors) { SelectUserCommand command = (SelectUserCommand) object; if (command.getUserId() == null || command.getUserId().trim().length() == 0) errors.reject("userId"); ValidationUtils.rejectIfEmpty(errors, "userId", "userId.empty"); System.out.println("UserID [" + command.getUserId() + "]"); if (command.getBegin() != null) { String temp = command.getBegin(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { sdf.parse(temp); } catch (ParseException e) { errors.rejectValue("begin", "If not null, then must be in yyyy-MM-dd format"); } } if (command.getEnd() != null) { String temp = command.getEnd(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { sdf.parse(temp); } catch (ParseException e) { errors.rejectValue("end", "If not null, then must be in yyyy-MM-dd format"); } } } }
Le SelectUserCommand :
Mon fichier de configuration spring contient
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 public class SelectUserCommand { private String _userId; private String _begin; private String _end; public String getUserId() { return _userId; } public void setUserId(String userId) { _userId = userId; } public String getBegin() { return _begin; } public void setBegin(String _begin) { this._begin = _begin; } public String getEnd() { return _end; } public void setEnd(String _end) { this._end = _end; } }
Et mon controller est :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <bean id="replaySelectController" class="be.manex.jafar.server.mvc.replay.SelectUserController"> <property name="commandName" value="command"/> <property name="commandClass" value="be.manex.jafar.server.mvc.replay.SelectUserCommand"/> <property name="formView" value="admin/replaySelect"/> <property name="validator" ref="selectUserValidator" /> <property name="validateOnBinding" value="true"/> </bean> <bean id="selectUserValidator" class="be.manex.jafar.server.mvc.replay.SelectUserValidator"/>
Et le resultat est qu'il passe finalement dans le /admin/replayList .. mais avec des mauvaise valeurs ...
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 public class SelectUserController extends SimpleFormController { protected ModelAndView processFormSubmission(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object, BindException bindException) throws Exception { SelectUserCommand command = (SelectUserCommand) object; Map<String, Object> map = new HashMap<String, Object>(); /* Divers traitements */ map.put("userId", command.getUserId()); map.put("begin", command.getBegin()); map.put("end", command.getEnd()); return new ModelAndView("/admin/replayList", map); } }
Et je dois dire que je rame pour trouver pq il passe quand meme ..
Ne devrait il pas stopper et affiche des erreurs ?
PS : je découvre le framework spring durant mon stage, mais meme un collegue qui travaille ici ne comprends pas pq ca foire...
J'espere que vous pourrez m'éclairer
Partager