在一个动作中将springmvc jsr303验证器与spring WebDataBinder验证器共存 [英] springmvc jsr303 validator co-exist with spring WebDataBinder validator in one action

查看:104
本文介绍了在一个动作中将springmvc jsr303验证器与spring WebDataBinder验证器共存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于springmvc 3.x现在支持jsr303和旧的Spring样式验证器,因此我想在示例应用程序中混合使用它们.但是对于指定的控制器仅启用一种方法,是spring框架或JSR标准的限制吗?

Since springmvc 3.x now supports jsr303 and old spring style validator, i want to mix them in my sample apps. But there is only one method enabled for a specified controller, is that the limit of spring framework or JSR standard?

这是我的示例代码.

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

User.java, stands for the domain model, uses JSR303 for validation.

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接口以支持用户验证.

UserValidator.java, implements the org.springframework.validation.Validator interface to support user validation.

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中.

UserController.java, uses InitBinder annotation to inject UserValidator into 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验证器进行验证. 谁能给我一种解决方法,将它们混合在一个控制器中?

If I uncomment the @InitBinder("user") in UserController, the JSR303 validation will be disabled. While the current commented code will use JSR validator to do the validation. Can anyone give me a workaround to mix them in one controller?

推荐答案

您可以直接使用验证器,也可以让全局LocalValidatorFactoryBean(JSR-303)发挥作用:

You can use your validator directly and let the global LocalValidatorFactoryBean (JSR-303) do its work as well:

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

这篇关于在一个动作中将springmvc jsr303验证器与spring WebDataBinder验证器共存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆