JSR-303验证在spring mvc 3.x中不起作用

时间:2012-06-19 06:23:39

标签: spring-mvc

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) {

   System.out.println("addEmployee employeeDTO! "+ employeeDTO);
       System.out.println("result.getErrorCount()  "+result.getErrorCount());

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;

}

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;

}

1 个答案:

答案 0 :(得分:0)

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

如果要将空输入字段绑定为null(并通过验证拒绝),则可以在活页夹中注册StringTrimmerEditor