有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从未调用java Spring验证程序

我试图验证一些信息,因此我添加了一个验证器,并在post方法的参数中使用@Valid:

@Controller
@RequestMapping("/user.htm")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String userInfo(Model model) {
       ....
        return "user";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String userInfoResult(@Valid @ModelAttribute UserForm userForm, BindingResult result, Model model ) {

        UserInfo stat = userService.getStatitisque(userForm.getSearchCritera());
        userForm.setListeExpediteur(listeExpediteur);

        userForm.setUserInfo(stat);
        model.addAttribute("userForm", userForm);
    }    
}

public class UserFormValidator implements Validator {

    @Override
    public boolean supports(Class<?> type) {
        return UserForm.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        UserForm userForm = (User) o;
        ...
    }
}

调试时,我从不使用UserFormValidator类

我需要在这些文件中添加一些内容吗

  • web.xml
  • applicationContext.xml
  • dispatcher-servlet.xml

共 (4) 个答案

  1. # 1 楼答案

    您需要在@InitBinder方法中添加验证器

     @InitBinder
     protected void initBinder(WebDataBinder binder) {
         binder.setValidator(new UserFormValidator ());
     }
    
  2. # 2 楼答案

    @Controller
    @RequestMapping("/user.htm")
    public class UserController {
    @Autowired
    private IUserService userService;
    
    @RequestMapping(method = RequestMethod.GET)
    public String userInfo(Model model) {
       ....
        return "user";
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String userInfoResult(Model model,
                                 @Valid @ModelAttribute UserForm userForm,
                                 BindingResult result) {
        /*
           add custom validation check to standard validation error
           (if not registry UserFormValidator in @InitBinder block)
        */
        new UserFormValidator().validate(userForm, error); 
    
        // check all validation errors   
        if (errors.hasErrors()) {
            // go back
            return userInfo(model);
        }
    
        ...
      } 
    }
    
  3. # 3 楼答案

    您需要在@InitBinder方法中添加验证器:

    @InitBinder(value="YourFormObjectName")
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new FooValidator());
    }
    

    或通过XML进行全局访问:

    <mvc:annotation-driven validator="globalValidator"/>
    

    参考资料:

  4. # 4 楼答案

    5.7.4.3配置JSR-303验证器供Spring MVC使用

    With JSR-303, a single javax.validation.Validator instance typically validates all model objects that declare validation constraints. To configure a JSR-303-backed Validator with Spring MVC, simply add a JSR-303 Provider, such as Hibernate Validator, to your classpath. Spring MVC will detect it and automatically enable JSR-303 support across all Controllers.

    <mvc:annotation-driven/>
    

    With this minimal configuration, anytime a @Valid @Controller input is encountered, it will be validated by the JSR-303 provider. JSR-303, in turn, will enforce any constraints declared against the input. Any ConstraintViolations will automatically be exposed as errors in the BindingResult renderable by standard Spring MVC form tags.