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

时间:2012-01-11 12:58:23

标签: spring validation 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
}

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;
    }

}

UserController.java,使用InitBinder注释将UserValidator注入WebDataBinder

@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";
    }
}

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

2 个答案:

答案 0 :(得分:8)

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

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

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

答案 1 :(得分: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";
        }
    }