How to turn on annotation driven validation in Spring 4?

后端 未结 2 980
耶瑟儿~
耶瑟儿~ 2020-12-10 21:10

I am using the annotation validation as below:

    public String processRegistration(@Valid Spitter spitter, Errors errors,
            Model model) {
               


        
2条回答
  •  情话喂你
    2020-12-10 21:38

    I see two points in your code that may cause de problem.

    1) Instead of use the correct namespace .

    2) On your @Controller change your functions parameters from:

       public String processRegistration(@Valid Spitter spitter, Errors errors,
            Model model) {
        if (errors.hasErrors()) {
            return "registerForm";
        }
        ...
    

    To:

    public String processRegistration(@ModelAttribute("spitter") @Valid Spitter spitter, BindingResult result) {
    
        if (result.hasErrors()) {
            return "registerForm";
        }
        ...
    

    Try it! ;)

提交回复
热议问题