0
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
4

1 回答 1

0

根据您引用的控制台消息,DTO 似乎并非无效。和字段似乎设置为空字符串,firstName而不是;因此,验证没有理由拒绝您的 bean。lastName""null

如果您希望将空输入字段绑定为 null(并被验证拒绝),您可以在活页夹中注册一个StringTrimmerEditor

于 2012-06-19T06:28:09.943 回答