步骤一 分组的定义
package com.test.validator.entity;
public interface GroupOne {
}
package com.test.validator.entity;
public interface GroupTwo {
}
步骤二:校验规则中 设置分组
@Size(min=1,max=14,groups= {GroupOne.class},message="{user.name.length.error}")
@NotEmpty(groups= {GroupOne.class})
private String userName;
private String password;
private String password_safe;
private String address;
private String birthday;
private String homepage;
@Email(groups= {GroupTwo.class})
private String email;
private String phone;
步骤三: 实例中实际应用分组校验
@RequestMapping(value="add")
public void add(HttpServletRequest request,HttpServletResponse response,@Validated(value=GroupTwo.class) User user, BindingResult bindingResult) {
List<ObjectError> allErrors = bindingResult.getAllErrors();
for(ObjectError e : allErrors){
System.out.println(e.getDefaultMessage());
}
}