SpringMVC數據校驗

1、數據校驗java

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


2、關鍵步驟:web

①、導入JAR包正則表達式

SpringMVC支持JSR(Java Specification Result,Java規範提案)303-Bean Validation數據驗證規範。而該規範的實現者不少,其中較經常使用的是Hibernate Validator。須要注意的是,Hibernate Validator是與Hibernate ORM並列的Hibernate的產品之一。這一點從Hibernate官網上所提供的資源形式能夠看出他們之間的關係。安全

 ②applicationContext.xml中配置驗證器服務器

③定義實體類,打註解標記app

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;
}

注:jsp

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

註解lua

適用的數據類型

說明

@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頁面

 <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>

 實現效果: 

若都不進行輸入:

 

若輸入合法:

 

相關文章
相關標籤/搜索