我有以下嵌套對象。我在我的控制器中使用@Valid進行驗證。這裏BindingResult對象不驗證Child對象的名稱字段。我錯過了什麼?Spring MVC 3嵌套對象驗證失敗@Valid
class Parent{
@Valid
private Child child;
//getter and setter for child object
}
class Child{
@NotNull(messag="Name cannot be null")
private String name;
//getter and setter for name
}
My controller validate method
@RequestMapping(value = "/validate", method = RequestMethod.POST)
public @ResponseBody String validate(@Valid @ModelAttribute("parent") Parent parent, BindingResult bindingResult) {
//Here I can see child name value if I say parent.getChild().getName()
// But if parent.getChild().getName() is null, bindingResult.hasErrors() is returning false
}