validation-api-1.0.0.GA.jar 和 hibernate-validator-4.1.0.Final.jar 在类路径中。 mvc:annotation-driven 在 -servlet.xml 文件中。
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public ModelAndView addEmployee(@Valid EmployeeDTO employeeDTO,
BindingResult result)
{
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