Spring Validate 數據驗證 參數驗證 SpringMVC—JSR303—validate


SpringMVC—JSR303—validate

  (2014-04-17 12:50:47)
分類: SpringMVC
定義:JSR-303 是JAVA EE 6 中的一項子規范,叫做Bean Validation,現在一共有兩個規范:BeanValidation1.0(即JSR303)和BeanValidation1.1(即JSR349),主要用於對數據進行校驗,確保輸入進來的數據從語 義上來講是正確的。
特點:1.JSR 303 用於對Java Bean 中的字段的值進行驗證,使得驗證邏輯從業務代碼中脫離出來。
                    2.是一個運行時的數據驗證框架,在驗證之后驗證的錯誤信息會被馬上返回。
應用場景:一般用於表單提交頁面(如用戶名必填、只能由數字字母組成等等)

使用:                                                                           
-----------------------------------------------------------------------------------------------------------------
1.@NotNull/@Null               
    驗證字段: 引用數據類型           
    注解說明:注解元素必須是非空或空
2.@Digits
    驗證字段:byte、short、int、long及各自的包裝類型以及BigDecimal、BigInteger、String
    注解說明:驗證數字構成是否合法
    屬性說明:integer:指定整數部分數字位數,fraction:指定小數部分數字位數
3.@Future/Past
    驗證字段:java.util.Date,java.util.Calendar
    注解說明:驗證是否在當前系統時間之后/之前
4.@Max/@Min
    驗證字段:byte、short、int、long及對應的包裝類型以及BigDecimal、BigInteger
    注解說明:驗證值是否小於等於最大指定整數值或大於等於最小指定整數值
5.@Pattern
    驗證字段:String
    注解說明:驗證字符串是否匹配指定的正則表達式
    屬性說明:regexp:匹配的正則表達式,flags:指定Pattern.Flag的數值,表示正則表達式的選項
6.@Size
    驗證字段:String、Collection、Map和數組
    注解說明:驗證元素大小是否在指定范圍內
    屬性說明:max:最大長度,min:最小長度,message:提示信息,默認:{constraint.size}
7.@DecimalMax/@DecimalMin
    驗證字段:byte、short、int、long及對應的包裝類型以及BigDecimal、BigInteger、String
    屬性說明:驗證值是否小於等於最大指定小數值或大於等於最小指定小數值
8.@Valid
    屬性說明:驗證值是否需要遞歸調用
---------------------------------------------------------------------------------------------------
Hibernate Validator 附加的 constraint
9.@Email    被注釋的元素必須是電子郵箱地址
10.@Length                    被注釋的字符串的大小必須在指定的范圍內
11.@NotEmpty    被注釋的字符串的必須非空
12.@Range                    被注釋的元素必須在合適的范圍內
-----------------------------------------------------------------------------------------------------------------
hibernate validate:
相關jar包:1.下載jar包4.0.2版本的jar文件
     2.解壓下載的壓縮包hibernate-validator和validation-api jar包
                                注意:這里面既包含了javax下的約束也包含了org.hibernate 下的約束,在之前的版本中,javax下的約束hibernate是不認的。
一、約束種類:
A:字段級的約束:
class Person(){
    @NotNull
    private Integer id
}
當約束被定義在字段上的時候, 這個字段的值是通過字段訪問策略來獲取並驗證的. 也就是說Bean Validation的實現者會直接訪問這個實例變量而不會調用屬性的訪問器(getter) 即使這個方法存在。靜態字段或者屬性是不會被校驗的

B:屬性級的約束
class Person(){
    @NotNull
    private String getUsername(){
    }
}
必須遵守JavaBeans規范,且定義在getter上,不能定義在setter上

C:類級的約束
D:約束的繼承
    public class Cart extends Animal{
     @NotNull
             private String name
    }
    不僅會驗證Cart中的約束,同時會驗證Animal中的約束

E:對象圖
     @Valid
            private Person person; 一個類中有一個對象屬性

二、約束的校驗   
    接口:Validator    校驗的主要接口
    怎么獲取實例對象?
     ValidatorFactory factory = Validation.buildDefaultValidatorFac tory();
     Validator validator =    factory.getValidator();
    調用實例對象中的方法返回Set對象,用來校驗整個實體對象或者對象的屬性
   
                 validate():對一個給定的實體對象中定義的所有約束進行校驗    Cart cart = new Cart(null, false, 5);   
                                                 Set> constraintViolations = validator.validate(cart);
                validateProperty():通過validateProperty()可以對一個給定實體對象的單個屬性進行校驗,需要符合JavaBean命名規范.Cart cart = new Cart(null, false, 5);
                                                Set> constraintViolations = validator.validateProperty(car, "manufacturer");
                validateValue() :通過validateValue() 方法,你能夠校驗如果把一個特定的值賦給一個類的某一個屬性的話,是否會違反此類中定義的約束條件. Cart cart = new Cart(null, false, 5);
                                                Set> constraintViolations = validator.validateValue(Car.class, "manufacturer", null);
    約束提示信息
     可以直接通過message=”提示信息”;也可以通過message提供模板,具體的錯誤信息在ValidationMessages.properties(src目錄下)中定義

注解

適用的數據類型

說明

@AssertFalse

Boolean, boolean

驗證注解的元素值是false

@AssertTrue

Boolean, boolean

驗證注解的元素值是true

@DecimalMax(value=x)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

驗證注解的元素值小於等於@ DecimalMax指定的value值

@DecimalMin(value=x)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

驗證注解的元素值小於等於@ DecimalMin指定的value值

@Digits(integer=整數位數, fraction=小數位數)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

驗證注解的元素值的整數位數和小數位數上限

@Future

java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

驗證注解的元素值(日期類型)比當前時間晚

@Max(value=x)

BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number.

驗證注解的元素值小於等於@Max指定的value值

@Min(value=x)

BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number.

驗證注解的元素值大於等於@Min指定的value值

@NotNull

Any type

驗證注解的元素值不是null

@Null

Any type

驗證注解的元素值是null

@Past

java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

驗證注解的元素值(日期類型)比當前時間早

@Pattern(regex=正則表達式, flag=)

String. Additionally supported by HV: any sub-type of CharSequence.

驗證注解的元素值與指定的正則表達式匹配

@Size(min=最小值, max=最大值)

String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence.

驗證注解的元素值的在min和max(包含)指定區間之內,如字符長度、集合大小

@Valid

Any non-primitive type(引用類型)

驗證關聯的對象,如賬戶對象里有一個訂單對象,指定驗證訂單對象

@NotEmpty

CharSequence,CollectionMap and Arrays

驗證注解的元素值不為null且不為空(字符串長度不為0、集合大小不為0)

@Range(min=最小值, max=最大值)

CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types

驗證注解的元素值在最小值和最大值之間

@NotBlank

CharSequence

驗證注解的元素值不為空(不為null、去除首位空格后長度為0),不同於@NotEmpty,@NotBlank只應用於字符串且在比較時會去除字符串的空格

@Length(min=下限, max=上限)

CharSequence

驗證注解的元素值長度在min和max區間內

@Email

CharSequence

驗證注解的元素值是Email,也可以通過正則表達式和flag指定自定義的email格式







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM