This example is a bit contrived; I\'ve simplified it to remove extraneous details and to focus on the problem I am having. I have a validator that looks like this:
In Spring, you need to obtain ValidatorFactory (or Validator itself) via LocalValidatorFactoryBean instead of Validation.buildDefaultValidatorFactory(), as described in the reference.
@Autowired
Validator validator;
@RequestMapping
public void checkUsername(Model model, User user) {
Set> constraintViolations = validator.validateProperty(user, "username");
model.addAttribute("error", constraintViolations.size() > 0);
}
-
EDIT: But perhaps the better way to do it is to use Spring MVC's automatic validation with @Valid annotation:
@RequestMapping
public void checkUsername(Model model, @Valid User user, BindingResult result) {
if (result.hasErrors()) {
...
}
}
This also requires in the config.