2012-06-19 27 views
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 

回答

0

的DTO似乎並不爲無效,根據您所引用的控制檯消息。 firstNamelastName字段似乎設置爲空字符串"",而不是null;因此,驗證沒有理由拒絕你的bean。

如果您希望將空輸入字段綁定爲空(並通過驗證被拒絕),您可以在活頁夾中註冊StringTrimmerEditor

+0

現在它的working.but結合自定義驗證與控制器中的JSR-303驗證然後JSR-303驗證不起作用@ InitBinder保護無效initBinder(WebDataBinder聯編程序){binder.registerCustomEditor(String.class,new StringTrimmerEditor(真正)); binder.setValidator(new EmailValidator());} – user739115