Note that there are some explanatory texts on larger screens.

plurals
  1. POspringmvc jsr303 validator co-exist with spring WebDataBinder validator in one action
    primarykey
    data
    text
    <p>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?</p> <p>Here is my sample code.</p> <p>User.java, stands for the domain model, uses JSR303 for validation. </p> <pre><code>public class User{ @Size(max = 16, message = "user loginId max-length is 16") private String loginId; //omit getter and setter } </code></pre> <p>UserValidator.java, implements the <code>org.springframework.validation.Validator</code> interface to support user validation.</p> <pre><code>public class UserValidator implements Validator { private UserService userService; public boolean supports(Class&lt;?&gt; 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 &amp;&amp; !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; } } </code></pre> <p>UserController.java, uses <code>InitBinder</code> annotation to inject <code>UserValidator</code> into <code>WebDataBinder</code>.</p> <pre><code>@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"; } } </code></pre> <p>If I uncomment the <code>@InitBinder("user")</code> 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? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload