I can't seem to get basic validation working with Spring MVC.
I have the following POJO:
public class Example {
@NotNull(message="You must enter a value")
@Size(min=2, max="2", message="You must enter 2 characters")
private String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
My very basic controller looks like this:
@Controller
@RequestMapping("/value.vm")
public class ValueController {
@Autowired
private Validator validator;
@RequestMapping()
public ModelAndView display(@Valid @ModelAttribute("example") Example e, BindingResult result) {
//Details omitted
}
My velocity template is equally as simple:
<form target="_self" enctype="application/x-www-form-urlencoded" action="/value.vm" method="post">
#springBind("example")
#springFormInput("example.value", "")
#springShowErrors("<br />", "")
</form>
My servlet.xml file includes:
<mvc:annotation-driven />
If I manually validate a blank form submission (i.e. validator.validate(e)), I see that my error messages are being generated, however they never exist on my BindingResult.
Any ideas why my binding result is not getting these errors?