springmvc jsr303验证器在一个动作中与spring WebDataBinder验证器共存

Big*_*fei 5 validation spring spring-mvc bean-validation

由于springmvc 3.x现在支持jsr303和旧的spring style验证器,我想在我的示例应用程序中混合它们.但是只有一个方法为指定的控制器启用,是弹簧框架或JSR标准的限制吗?

这是我的示例代码.

User.java代表域模型,使用JSR303进行验证.

public class User{
    @Size(max = 16, message = "user loginId max-length is 16")
    private String loginId;
    //omit getter and setter
}
Run Code Online (Sandbox Code Playgroud)

UserValidator.java实现了org.springframework.validation.Validator支持用户验证的接口.

public class UserValidator implements Validator {

    private UserService userService;
    public boolean supports(Class<?> clazz) {
        return User.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
            User u = (User) target;

            // loginName check for new user
            if (u.getUserId() == null && !userService.isLoginIdUnique(u.getLoginId(), null)) {
                    errors.rejectValue("loginId", "user.loginId.unique", new Object[] { u.getLoginId() }, null);
            }
    }

    @Autowired
    public void setUserService(UserService userService) {
            this.userService = userService;
    }

}
Run Code Online (Sandbox Code Playgroud)

UserController.java,使用InitBinder注释注入UserValidatorWebDataBinder.

@Controller("jspUserController")
@RequestMapping("/sys/users")
public class UserController {
    private UserValidator userValidator;

    @Autowired
    public void setUserValidator(UserValidator userValidator) {
        this.userValidator = userValidator;
    }

/*@InitBinder("user")
    public void initBinderUser(WebDataBinder binder) {
        binder.setValidator(userValidator);
    }*/

    @RequestMapping(value = "/save")
    public String save(@Valid User user, BindingResult bindingResult, Model model, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return "/sys/user/edit";
        }
        userService.saveUser(user);
        return "redirect:/sys/users/index";
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我@InitBinder("user")在UserController中取消注释,则将禁用JSR303验证.虽然当前注释的代码将使用JSR验证器来进行验证.任何人都可以给我一个解决方法,将它们混合在一个控制器中?

小智 8

您可以添加验证器而不是设置它:

@InitBinder("user")
public void initBinderUser(WebDataBinder binder) {
    binder.addValidators(userValidator);
}
Run Code Online (Sandbox Code Playgroud)

这将首先执行JSR303验证,然后执行自定义验证器.然后无需直接在save方法中调用验证器.


Ros*_*hev 6

您可以直接使用验证器,并让全局LocalValidatorFactoryBean(JSR-303)也能正常工作:

    @Controller("jspUserController")
    @RequestMapping("/sys/users")
    public class UserController {
        private UserValidator userValidator;

        @Autowired
        public void setUserValidator(UserValidator userValidator) {
            this.userValidator = userValidator;
        }

        @RequestMapping(value = "/save")
        public String save(@Valid User user, BindingResult bindingResult, Model model, HttpServletRequest request) {
            this.userValidator.validate(user, bindingResult);
            if (bindingResult.hasErrors()) {
                return "/sys/user/edit";
            }
            userService.saveUser(user);
            return "redirect:/sys/users/index";
        }
    }
Run Code Online (Sandbox Code Playgroud)