IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Web Java Discussion :

[Spring MVC][Validator] Ne stop pas si il y a une erreurs


Sujet :

Spring Web Java

  1. #1
    Rédacteur
    Avatar de Hikage
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    1 177
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 1 177
    Points : 6 301
    Points
    6 301
    Par défaut [Spring MVC][Validator] Ne stop pas si il y a une erreurs
    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 :
    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;
        }
     
    }
    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
     
     
        <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 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
    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 le resultat est qu'il passe finalement dans le /admin/replayList .. mais avec des mauvaise valeurs ...

    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
    Hikage
    SCJP / SCWCD & SCWSJD Certified / Spring Framework Certified
    [Personal Web] [CV]

    F.A.Q Spring Framework - Participez !

  2. #2
    Membre averti
    Inscrit en
    Août 2005
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 352
    Points : 427
    Points
    427
    Par défaut
    Tu surcharges la mauvaise méthode et tu perds tout ce que cette méthode est censée faire :
    Citation Envoyé par javadoc
    This implementation calls showForm in case of errors, and delegates to onSubmit's full version else.

    This can only be overridden to check for an action that should be executed without respect to binding errors, like a cancel action. To just handle successful submissions without binding errors, override one of the onSubmit methods or doSubmitAction.
    Surcharge la méthode onSubmit.

  3. #3
    Membre averti
    Inscrit en
    Août 2005
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 352
    Points : 427
    Points
    427
    Par défaut
    Je parlais de la méthode processFormSubmission

  4. #4
    Rédacteur
    Avatar de Hikage
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    1 177
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 1 177
    Points : 6 301
    Points
    6 301
    Par défaut
    Merci ! Ca marche impec
    Hikage
    SCJP / SCWCD & SCWSJD Certified / Spring Framework Certified
    [Personal Web] [CV]

    F.A.Q Spring Framework - Participez !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Spring MVC] Validator : problème de rejectValue
    Par Fearless13 dans le forum Spring Web
    Réponses: 7
    Dernier message: 08/07/2010, 10h42
  2. Spring MVC Validation
    Par MASSAKA dans le forum Spring Web
    Réponses: 0
    Dernier message: 08/12/2008, 12h48
  3. [Spring MVC] onSubmit n'est pas exécuté
    Par napol1fr dans le forum Spring Web
    Réponses: 1
    Dernier message: 30/05/2008, 12h03
  4. [Spring MVC] onSubmit n'est pas éxecuté
    Par jamalmoundir dans le forum Spring Web
    Réponses: 1
    Dernier message: 27/06/2007, 15h19
  5. [Spring MVC] validation.xml coté client
    Par Tail dans le forum Spring Web
    Réponses: 1
    Dernier message: 28/10/2006, 17h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo