zoukankan      html  css  js  c++  java
  • Hibernate validation 注解 springmvc 验证 分组

     

    SpringMVC验证框架Validation特殊用法

     

    1. 分组

    有的时候,我们对一个实体类需要有多中验证方式,在不同的情况下使用不同验证方式,比如说对于一个实体类来的id来说,保存的时候是不需要的,对于更新时是必须的,可以如下配置:

    [java] view plain copy
     
    1. public class UserModel {  
    2.   
    3.     @NotNull(message = "{id.empty}", groups = { First.class })  
    4.     private int id;  
    5.   
    6.     @NotNull(message = "{username.empty}", groups = { First.class, Second.class })  
    7.     private String username;  
    8.   
    9.     @NotNull(message = "{content.empty}", groups = { First.class, Second.class })  
    10.     private String content;  
    11.   
    12.     public int getId() {  
    13.         return id;  
    14.     }  
    15.   
    16.     public void setId(int id) {  
    17.         this.id = id;  
    18.     }  
    19.   
    20.     public String getUsername() {  
    21.         return username;  
    22.     }  
    23.   
    24.     public void setUsername(String username) {  
    25.         this.username = username;  
    26.     }  
    27.   
    28.     public String getContent() {  
    29.         return content;  
    30.     }  
    31.   
    32.     public void setContent(String content) {  
    33.         this.content = content;  
    34.     }  
    35. }  
    36. public interface First {  
    37. }  
    38.   
    39. public interface Second {  
    40. }  

    通过 groups 对验证进行分组

    在controler中的代码如下:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @RequestMapping(value = "/save.action", method = RequestMethod.POST)  
    2. public String save(@Validated( { Second.class }) UserModel userModel, BindingResult result) {  
    3.     if (result.hasErrors()) {  
    4.         return "validate/error";  
    5.     }  
    6.     return "redirect:/success";  
    7. }  
    8.   
    9. @RequestMapping(value = "/update.action", method = RequestMethod.POST)  
    10. public String update(@Validated( { First.class, Second.class }) UserModel user, BindingResult result) {  
    11.     if (result.hasErrors()) {  
    12.         return "validate/error";  
    13.     }  
    14.     return "redirect:/success";  
    15. }  

    2. 组序列

    默认情况下,不同组别的约束验证是无序的,然而在某些情况下,约束验证的顺序却很重要,如下面两个例子:(1)第二个组中的约束验证依赖于一个稳定状态来运行,而这个稳定状态是由第一个组来进行验证的。(2)某个组的验证比较耗时,CPU 和内存的使用率相对比较大,最优的选择是将其放在最后进行验证。因此,在进行组验证的时候尚需提供一种有序的验证方式,这就提出了组序列的概念。

    一个组可以定义为其他组的序列,使用它进行验证的时候必须符合该序列规定的顺序。在使用组序列验证的时候,如果序列前边的组验证失败,则后面的组将不再给予验证。

    下例中声明了组 GroupA.class,GroupB.class 和 Group.class,其中 default,GroupA,GroupB 均为 Group 的序列。

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public interface GroupA {  
    2. }  
    3.   
    4. public interface GroupB {  
    5. }  
    6.   
    7. @GroupSequence( { Default.class, GroupA.class, GroupB.class })  
    8. public interface Group {  
    9. }  
    10.   
    11. public class User {  
    12.     @NotEmpty(message = "firstname may be empty")  
    13.     private String firstname;  
    14.   
    15.     @NotEmpty(message = "middlename may be empty", groups = Default.class)  
    16.     private String middlename;  
    17.   
    18.     @NotEmpty(message = "lastname may be empty", groups = GroupA.class)  
    19.     private String lastname;  
    20.   
    21.     @NotEmpty(message = "country may be empty", groups = GroupB.class)  
    22.     private String country;  
    23. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @RequestMapping(value = "/update.action", method = RequestMethod.POST)  
    2. public String register(@Validated(Group.class) User user, BindingResult result) {  
    3.     if (result.hasErrors()) {  
    4.         return "validate/error";  
    5.     }  
    6.     return "redirect:/success";  
    7. }  

    3. 验证多个对象

    当我们在一个功能处理方法上需要验证多个模型对象时,需要通过如下形式来获取验证结果:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @RequestMapping("/validate/multi")  
    2. public String multi(@Valid @ModelAttribute("a") A a, BindingResult aErrors, @Valid @ModelAttribute("b") B b, BindingResult bErrors) {  
    3.   
    4.     if (aErrors.hasErrors()) { //如果a模型对象验证失败  
    5.         return "validate/error";  
    6.     }  
    7.     if (bErrors.hasErrors()) { //如果a模型对象验证失败  
    8.         return "validate/error";  
    9.     }  
    10.     return "redirect:/success";  
    11. }  

    每一个模型对象后边都需要跟一个Errors或BindingResult对象来保存验证结果,其方法体内部可以使用这两个验证结果对象来选择出错时跳转的页面或处理的逻辑。

    4. Junit测试

    当自定义拓展Validation时,可以使用如下方法进行测试:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @Test  
    2. public void testValidate() {  
    3.     AnnotationDescriptor<EqualsAny> descriptor = new AnnotationDescriptor<EqualsAny>(EqualsAny.class);  
    4.     EqualsAny equalsAny = AnnotationFactory.create(descriptor);  
    5.     EqualsAnyValidator equalsAnyValidator = new EqualsAnyValidator();  
    6.     equalsAnyValidator.initialize(equalsAny);  
    7.     Assert.assertTrue(equalsAnyValidator.isValid("123", null));  
    8. }  

    另外再讲一点Spring对自定义JSR-303限制类型支持的新特性,那就是Spring支持往ConstraintValidator里面注入bean对象。例如在EqualsAnyValidator中利用@Resource注解注入其他Bean对象。

     

    在使用Validation时,传递参数到国际化资源文件properties

     

     在CODE上查看代码片派生到我的代码片
    1. @NotEmpty(message="{password.empty.error}")  
    2. private String password;  

    资源文件validation_zh_CN.properties中为

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. password.empty.error=password不能为空  

    实际开发中,很多参数都是要验证非空的,如果每个参数都单独加个错误描述,是很麻烦的。properties虽支持“{}”的写法传递参数,但使用JSR-303注解无法实现传递参数。我想了个办法可通过自定义注解方式实现。

    首先,建立个自定义的@NotEmpty注解:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.itkt.payment.core.annotation;  
    2.   
    3. import java.lang.annotation.ElementType;  
    4. import java.lang.annotation.Retention;  
    5. import java.lang.annotation.RetentionPolicy;  
    6. import java.lang.annotation.Target;  
    7.   
    8. import javax.validation.Constraint;  
    9. import javax.validation.Payload;  
    10.   
    11. import com.itkt.payment.core.handler.NotEmptyValidator;  
    12.   
    13. @Retention(RetentionPolicy.RUNTIME)  
    14. @Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER })  
    15. @Constraint(validatedBy = { NotEmptyValidator.class })  
    16. public @interface NotEmpty {  
    17.   
    18.     String field() default "";  
    19.   
    20.     String message() default "{com.itkt.payment.core.handler.NotEmpty.message}";  
    21.   
    22.     Class<?>[] groups() default {};  
    23.   
    24.     Class<? extends Payload>[] payload() default {};  
    25. }  

    自定义的NotEmpty注解中,我们新加了field字段,用于标识字段名称。

    然后,建立NotNullValidator实现类:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.itkt.payment.core.handler;  
    2.   
    3. import javax.validation.ConstraintValidator;  
    4. import javax.validation.ConstraintValidatorContext;  
    5.   
    6. import com.itkt.payment.core.annotation.NotNull;  
    7.   
    8. public class NotNullValidator implements ConstraintValidator<NotNull, Object> {  
    9.   
    10.     @Override  
    11.     public void initialize(NotNull annotation) {  
    12.     }  
    13.   
    14.     @Override  
    15.     public boolean isValid(Object str, ConstraintValidatorContext constraintValidatorContext) {  
    16.         return str != null;  
    17.     }  
    18.   
    19. }  

    之后,在资源文件validation_zh_CN.properties中,改变写法:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. password.empty.error={field}不能为空  

    最后,我们就可以在User类中使用自定义的NotEmpty注解:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @NotEmpty(field = "password", message = "{password.empty.error}")  
    2. private String password;  

    实际上,国际化资源文件本身支持从JSR-303注解中获取属性的参数值的,例如从@Length注解中,获取min和max属性的值:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. username.length.error=用户名长度必须在{min}-{max}之间  

    之所以自带的@NotEmpty注解无法实现,是因为没有一个属性能传递字段名,所以通过自定义@NotEmpty注解来拓展个field字段。

     
     
    @AssertTrue   //用于boolean字段,该字段只能为true   
    @AssertFalse //该字段的值只能为false   
    @CreditCardNumber //对信用卡号进行一个大致的验证   
    @DecimalMax //只能小于或等于该值   
    @DecimalMin //只能大于或等于该值   
    @Digits (integer= 2 ,fraction= 20 ) //检查是否是一种数字的整数、分数,小数位数的数字。   
    @Email //检查是否是一个有效的email地址   
    @Future //检查该字段的日期是否是属于将来的日期   
    @Length (min=,max=) //检查所属的字段的长度是否在min和max之间,只能用于字符串   
    @Max //该字段的值只能小于或等于该值   
    @Min //该字段的值只能大于或等于该值   
    @NotNull //不能为null   
    @NotBlank //不能为空,检查时会将空格忽略   
    @NotEmpty //不能为空,这里的空是指空字符串   
    @Null //检查该字段为空   
    @Past //检查该字段的日期是在过去   
    @Size (min=, max=) //检查该字段的size是否在min和max之间,可以是字符串、数组、集合、Map等   
    @URL (protocol=,host,port) //检查是否是一个有效的URL,如果提供了protocol,host等,则该URL还需满足提供的条件   
    @Valid //该注解只要用于字段为一个包含其他对象的集合或map或数组的字段,或该字段直接为一个其他对象的引用,   
           //这样在检查当前对象的同时也会检查该字段所引用的对象  

    以下是分类
    Bean Validation 中内置的 constraint
     
    @Null     被注释的元素必须为 null
    @NotNull     被注释的元素必须不为 null
    @AssertTrue     被注释的元素必须为 true
    @AssertFalse     被注释的元素必须为 false
    @Min(value)     被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @Max(value)     被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @DecimalMin(value)     被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @DecimalMax(value)     被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @Size(max=, min=)     被注释的元素的大小必须在指定的范围内
    @Digits (integer, fraction)     被注释的元素必须是一个数字,其值必须在可接受的范围内
    @Past     被注释的元素必须是一个过去的日期
    @Future     被注释的元素必须是一个将来的日期
    @Pattern(regex=,flag=)     被注释的元素必须符合指定的正则表达式

    Hibernate Validator 附加的 constraint
    @NotBlank(message =)   验证字符串非null,且长度必须大于0
    @Email     被注释的元素必须是电子邮箱地址
    @Length(min=,max=)     被注释的字符串的大小必须在指定的范围内
    @NotEmpty     被注释的字符串的必须非空
    @Range(min=,max=,message=)     被注释的元素必须在合适的范围内
  • 相关阅读:
    第二周教学设计反思
    uni-app,一套代码应用在多个平台
    新CMS
    学习js
    关于数据库
    自己写的一段jquery实现当滚动条滑动到某个位置实现向中间移动
    js平滑滚动到顶部、底部、指定地方
    统一一个帝国中搜索页面的模板
    帝国CMS的总结
    回到顶部的功能
  • 原文地址:https://www.cnblogs.com/hujihon/p/5357481.html
Copyright © 2011-2022 走看看