2012-06-19 4 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 검증은 인용 콘솔 메시지에 따라,

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가 잘못 될 것 같지 않습니다 스프링 MVC 3.X에서 작동하지 않습니다 . firstName 및필드는 null이 아닌 빈 문자열 ""으로 설정되는 것 같습니다. 따라서 검증을 통해 빈을 거부 할 이유가 없습니다.

빈 입력 필드를 null로 바인딩하고 (유효성 검사를 통해 거부하는 경우) 바인더에 StringTrimmerEditor을 등록 할 수 있습니다.

+0

이제 컨트롤러에서 JSR-303 유효성 검사와 함께 사용자 지정 유효성 검사와 결합 할 때 JSR-303 유효성 검사가 작동하지 않습니다. @ InitBinder protected void initBinder (WebDataBinder binder) {binder.registerCustomEditor (String.class, new StringTrimmerEditor 참된)); binder.setValidator (새 EmailValidator());} – user739115