0
validation-api-1.0.0.GA.jar and hibernate-validator-4.1.0.Final.jar are in classpath. mvc:annotation-driven is in -servlet.xml file.
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public ModelAndView addEmployee(@Valid EmployeeDTO employeeDTO, BindingResult result) {
JSR-303的驗證沒有Spring MVC中3.X工作
System.out.println("addEmployee employeeDTO! "+ employeeDTO);
System.out.println("result.getErrorCount() "+result.getErrorCount());
}
public class EmployeeDTO {
private int employeeId;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
@NotNull
private String firstName;
@NotNull
private String lastName;
private Date hireDate;
}
in the console
addEmployee employeeDTO! EmployeeDTO [employeeId=0, firstName=, lastName=, deptName=null, deptId=0, email=, salary=0, jobId=AD_VP, hireDate=null]
result.getErrorCount() 0
現在它的working.but結合自定義驗證與控制器中的JSR-303驗證然後JSR-303驗證不起作用@ InitBinder保護無效initBinder(WebDataBinder聯編程序){binder.registerCustomEditor(String.class,new StringTrimmerEditor(真正)); binder.setValidator(new EmailValidator());} – user739115