SpringMVC數據校驗


一、數據校驗

在web應用程序中,為了防止客戶端傳來的數據引發程序異常,常常需要對 數據進行驗證。輸入驗證分為客戶端驗證與服務器端驗證。客戶端驗證主要通過JavaScript腳本進行,而服務器端驗證則主要通過Java代碼進行驗證。 為了保證數據的安全性,一般情況下,客戶端和服務器端驗證都是必須的


二、關鍵步驟:

①、導入JAR包

SpringMVC支持JSR(Java Specification Result,Java規范提案)303-Bean Validation數據驗證規范。而該規范的實現者很多,其中較常用的是Hibernate Validator。需要注意的是,Hibernate Validator是與Hibernate ORM並列的Hibernate的產品之一。這一點從Hibernate官網上所提供的資源形式可以看出他們之間的關系。

 ②applicationContext.xml中配置驗證器

③定義實體類,打注解標記

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public  class  UserInfo {
     @NotEmpty (message= "用戶名不能為空" )
     @Size (min= 3 ,max= 6 ,message= "姓名長度應在{min}-{max}" )
     private  String username;
     
     @NotNull (message= "成績最大值為100" )
     @Min (value= 0 ,message= "成績不能小於{value}" )
     @Max (value= 100 ,message= "成績不能大於{value}" )
     private  Integer score;
     
     @NotEmpty (message= "手機號碼不允許為空" )
     @Pattern (regexp= "^1[34578]\\d{9}$" ,message= "手機號碼格式不正確" )
     private  String phone;
}

注:

下面是主要的驗證注解及說明:

注解

適用的數據類型

說明

@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格式

④書寫Controller方法

 

⑤搭建jsp頁面

1
2
3
4
5
6
7
<form action= "${pageContext.request.contextPath }/first.do"  method= "post" >
    <h1>數據驗證</h1>
                 姓名:<input name= "username" />${namemsg }<br/><br/>
                 成績:<input name= "score"  />${scoremsg}<br/><br/>
                 電話:<input name= "phone"  />${phonemsg }<br/><br/>
       <input type= "submit"  value= "注冊" />
    </form>

 實現效果: 

若都不進行輸入:

 

若輸入合法:

 


免責聲明!

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



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