程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 用好spring mvc validator可以簡化代碼,mvcvalidator

用好spring mvc validator可以簡化代碼,mvcvalidator

編輯:JAVA綜合教程

用好spring mvc validator可以簡化代碼,mvcvalidator


表單的數據檢驗對一個程序來講非常重要,因為對於客戶端的數據不能完全信任,常規的檢驗類型有:

  • 參數為空,根據不同的業務規定要求表單項是必填項
  • 參數值的有效性,比如產品的價格,一定不能是負數
  • 多個表單項組合檢驗,比如在注冊時密碼與確認密碼必須相同
  • 參數值的數據范圍,常見的是一些狀態值,或者叫枚舉值,如果傳遞的參數超出已經定義的枚舉那麼也是無意義的

上面的這些檢驗基本上都是純數據方面的,還不算具體的業務數據檢驗,下面是一些強業務相關的數據檢驗

  • 根據產品ID,去檢驗ID是否真實存在
  • 注冊用戶時,需要檢驗用戶名的唯一性
  • ....


根據上面的需求,如果我們將這些檢驗的邏輯全部與業務邏輯耦合在一起,那麼我們的程序邏輯將會變得冗長而且不便於代碼復用,下面的代碼就是耦合性強的一種體現:

           if (isvUserRequestDTO == null) {
                log.error("can not find isv request by request id, " + isvRequestId);
                return return_value_error(ErrorDef.FailFindIsv);
            }
            if (isvUserRequestDTO.getAuditStatus() != 1) {
                log.error("isv request is not audited, " + isvRequestId);
                return return_value_error(ErrorDef.IsvRequestNotAudited);
            }

 

我們可以利用spring提供的validator來解耦表單數據的檢驗邏輯,可以將上述的代碼從具體的業務代碼的抽離出去。

 


Hibernate validator,它是JSR-303的一種具體實現。它是基於注解形式的,我們看一下它原生支持的一些注解。

注解 說明 @Null 只能為空,這個用途場景比較少 @NotNull 不能為空,常用注解 @AssertFalse 必須為false,類似於常量 @AssertTrue 必須為true,類似於常量 @DecimalMax(value)   @DecimalMin(value)   @Digits(integer,fraction)   @Future 代表是一個將來的時間 @Max(value) 最大值,用於一個枚舉值的數據范圍控制 @Min(value) 最小值,用於一個枚舉值的數據范圍控制 @Past 代表是一個過期的時間 @Pattern(value) 正則表達式,比如驗證手機號,郵箱等,非常常用 @Size(max,min)

限制字符長度必須在min到max之間

 

 

 

 

 

 

 

 

 

 

 

 

 

 基礎數據類型的使用示例

@NotNull(message = "基礎數量不能為空")
    @Min(value = 0,message = "基礎數量不合法")
    private Integer baseQty;


嵌套檢驗,如果一個對象中包含子對象(非基礎數據類型)需要在屬性上增加@Valid注解。

   @Valid
   @NotNull(message = "價格策略內容不能為空")
    private List<ProductPricePolicyItem> policyItems;


除了原生提供的注解外,我們還可以自定義一些限制性的檢驗類型,比如上面提到的多個屬性之間的聯合檢驗。該注解需要使用@Constraint標注,這裡我編寫了一個用於針對兩個屬性之間的數據檢驗的規則,它支持兩個屬性之間的如下操作符,而且可以設置多組屬性對。

  • ==
  • >
  • >=
  • <
  • <=

創建注解

  • 通過@Constraint指定檢驗的實現類CrossFieldMatchValidator
  • 增加兩個屬性名稱字段,用於後續的檢驗
  • 增加一個注解的List,用來支持一個對象中檢驗多組屬性對。比如即需要檢驗最大數量與最小數量,也需要檢驗密碼與確認密碼
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = CrossFieldMatchValidator.class)
@Documented
public @interface CrossFieldMatch {

    String message() default "{constraints.crossfieldmatch}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    /**
     * @return The first field
     */
    String first();

    /**
     * @return The second field
     */
    String second();

    /**
     * first operator second
     * @return
     */
    CrossFieldOperator operator();

    /**
     * Defines several <code>@FieldMatch</code> annotations on the same element
     *
     * @see CrossFieldMatch
     */
    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        CrossFieldMatch[] value();
    }
}

檢驗實現類

isValid方法,通過反射可以取到需要檢驗的兩個字段的值以及數據類型,然後根據指定的數據操作符以及數據類型做出計算。目前這個檢驗只針對我的業務並不十分通用,需要根據自己的項目情況來靈活處理。

public class CrossFieldMatchValidator implements ConstraintValidator<CrossFieldMatch, Object> {

    private String firstFieldName;
    private String secondFieldName;
    private CrossFieldOperator operator;

    @Override
    public void initialize(final CrossFieldMatch constraintAnnotation) {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        operator=constraintAnnotation.operator();
    }

    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context) {
        try {
            Class valueClass=value.getClass();
            final Field firstField = valueClass.getDeclaredField(firstFieldName);
            final Field secondField = valueClass.getDeclaredField(secondFieldName);
            //不支持為null的字段
            if(null==firstField||null==secondField){
                return false;
            }

            firstField.setAccessible(true);
            secondField.setAccessible(true);
            Object firstFieldValue= firstField.get(value);
            Object secondFieldValue= secondField.get(value);

            //不支持類型不同的字段
            if(!firstFieldValue.getClass().equals(secondFieldValue.getClass())){
                return false;
            }

            //整數支持 long int short
            //浮點數支持 double
            if(operator==CrossFieldOperator.EQ) {
                return firstFieldValue.equals(secondFieldValue);
            }
            else if(operator==CrossFieldOperator.GT){
                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {
                    return (Long)firstFieldValue > (Long) secondFieldValue;
                }
                else if(firstFieldValue.getClass().equals(Double.class)) {
                    return (Double)firstFieldValue > (Double) secondFieldValue;
                }

            }
            else if(operator==CrossFieldOperator.GE){
                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {
                    return Long.valueOf(firstFieldValue.toString()) >= Long.valueOf(secondFieldValue.toString());
                }
                else if(firstFieldValue.getClass().equals(Double.class)) {
                    return Double.valueOf(firstFieldValue.toString()) >= Double.valueOf(secondFieldValue.toString());
                }
            }
            else if(operator==CrossFieldOperator.LT){
                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {
                    return (Long)firstFieldValue < (Long) secondFieldValue;
                }
                else if(firstFieldValue.getClass().equals(Double.class)) {
                    return (Double)firstFieldValue < (Double) secondFieldValue;
                }
            }
            else if(operator==CrossFieldOperator.LE){
                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {
                    return Long.valueOf(firstFieldValue.toString()) <= Long.valueOf(secondFieldValue.toString());
                }
                else if(firstFieldValue.getClass().equals(Double.class)) {
                    return Double.valueOf(firstFieldValue.toString()) <= Double.valueOf(secondFieldValue.toString());
                }
            }
        }
        catch (final Exception ignore) {
            // ignore
        }
        return false;
    }
}

 

調用示例:

@CrossFieldMatch.List({
        @CrossFieldMatch(first = "minQty", second = "maxQty",operator = CrossFieldOperator.LE ,message = "最小數量必須小於等於最大數量")
})
public class ProductPriceQtyRange implements Serializable{
    /**
     * 最小數量
     */
    @Min(value = 0,message = "最小數量不合法")
    private int minQty;
    /**
     * 最大數量
     */
    @Min(value = 0,message = "最大數量不合法")
    private int maxQty;

    public int getMinQty() {
        return minQty;
    }

    public void setMinQty(int minQty) {
        this.minQty = minQty;
    }

    public int getMaxQty() {
        return maxQty;
    }

    public void setMaxQty(int maxQty) {
        this.maxQty = maxQty;
    }
}

 


需要在mvc的配置文件中增加如下節點以啟動檢驗

 <mvc:annotation-driven validator="validator">

  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="useCodeAsDefaultMessage" value="false"/>  
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

經過上面在對象屬性上的數據檢驗注解,我們將大部分的數據檢驗邏輯從業務邏輯中轉移出去,不光是精簡了代碼還使得原本復雜的代碼變得簡單清晰,代碼的重復利用率也增強了。

 

本文引用:

http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303

 



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved