springmvc jsr303 validator co-exist with spring WebDataBinder validator in one action

北城以北 提交于 2019-12-07 04:20:23

问题


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?

Here is my sample code.

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

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?


回答1:


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



回答2:


You can ADD your validator instead of SETTING it :

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

This will execute the JSR303 validations first and then your custom validator. No need then to call the validator directly in the save method.



来源:https://stackoverflow.com/questions/8819676/springmvc-jsr303-validator-co-exist-with-spring-webdatabinder-validator-in-one-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!