使用spring @Valid 驗證時 使用了 Groups屬性java
User.javaspring
@NotBlank(message="id 未填" ,groups={Group.A.class}) private String id; @NotBlank(message="name 未填" ) private String name;
spring 驗證app
@RequestMapping("/Add") public String shippingSingleAdd(HttpServletRequest request ,@Valid User user, BindingResult result) { if(result.hasErrors()){ return "error"; } userService.Add(user); return "success"; }
會產生問題spa
id | 沒有驗證 |
name | 驗證了 |
解決方法code
@NotBlank(message="id 未填" ,groups={Group.A.class, Default.class}) //javax.validation.groups.Default private String id; @NotBlank(message="name 未填") private String name;