1、在ActionSupport中有一個validate()方法,這個方法是驗證方法,它會在execute()方法執(zhí)行之前執(zhí)行,所以能夠起到很好的驗證的作用。
1
2
3
4
5
6
|
@Override //重寫Action中的validate()方法 public void validate() { if ( null == this .username|| this .username.length()< 4 || this .username.length()> 6 ){ this .addActionError( "username invadate" ); } } |
a、如果驗證沒有通過,我們可以調(diào)用addActionError("Error Message");這樣,這個錯誤信息就被保存了。
validate整個方法都執(zhí)行完成之后,系統(tǒng)就會自動去檢查name="input"所對應(yīng)的jsp頁面,一般建議跳到我們注冊的頁面,即哪里來,回哪里去
1
2
|
/registerResult.jsp /register.jsp |
b、然后我們在register.jsp
即最初的注冊頁面添加上這個struts2標(biāo)簽
1
|
<s:actionerror cssStyle= "color:red" /> |
表示的意思是:如果存在錯誤信息,即actionerror存在,則輸出這個錯誤信息,
而且,我們可以對標(biāo)簽進行css的設(shè)置
1
2
|
//注意,使用struts2標(biāo)簽必須在頭文件中引入標(biāo)簽: <%@ taglib prefix= "s" uri= "/struts-tags" %> |
c、
一種比較日期的方法:
brithday與graduate為Date類型
1
2
3
4
5
6
7
8
9
10
11
|
if ( null != birthday && null != graduation) { Calendar c1 = Calendar.getInstance(); c1.setTime(birthday); Calendar c2 = Calendar.getInstance(); c2.setTime(graduation); if (!c1.before(c2)) { this .addActionError( "birthday should be before graduation" ); } } |
2、Action級別與Field級別。通過這種方式,讓我們可以在添加錯誤的時候可以往不同的級別添加,提示錯誤信息的時候可以更靈活,而不會把所有的信息都添加到Action級別以后,所有的信息都同一顯示出來。比如說:我們要把重復(fù)密碼錯誤這個錯誤信息用紅色的字體表示,而其他信息,比如說用戶名、年齡等信息用綠色的字體來表示,這個時候通過使用往不同的級別添加就可以了。
在注冊頁面,也只要寫一句Field級別的標(biāo)簽就可以了。如下:
1
2
3
4
5
6
7
8
|
<s:actionerror cssStyle= "color:red" /> //action級別 <s:fielderror cssStyle= "color:blue" ></s:fielderror> //field級別 public void validate() { if ( null == this .username|| this .username.length()< 4 || this .username.length()> 6 ){ this .addActionError( "username invadate" ); //往Action級別添加錯誤信息 this .addFieldError( "username" , "username invadate in field" ); //往field級別處添加錯誤信息 } } |
3、發(fā)送錯誤后,將原來的信息還顯示在表單里面。
1
2
3
4
5
6
7
8
9
|
<s:form action= "RegisterAction" > <s:textfield name= "username" label= "username" ></s:textfield> <s:password name= "password" label= "password" ></s:password> <s:password name= "repassword" label= "repassword" ></s:password> <s:textfield name= "age" label= "age" ></s:textfield> <s:textfield name= "birthday" label= "brithday" ></s:textfield> <s:textfield name= "graduate" label= "graduate" ></s:textfield> <s:submit value= "submit" ></s:submit> </s:form> |
使用struts2標(biāo)簽,能夠自動排版,然后能夠?qū)㈠e誤的Field級別的信息顯示出來,如下所示。
4、不過這種自動使用table來排版的方式雖然方便,但是很多情況下不符合我們的需求,所以我們可以使用自定義的排版方式。
方法二:定義排版方式為simple,這樣子我們就可以按照html的方式來自己排版了 。
field級別的錯誤也不會被自動顯示出來。
1
2
3
4
5
6
7
8
9
|
<s:form action= "RegisterAction" theme= "simple" ><br/> username:<s:textfield name= "username" label= "username" ></s:textfield><br/> password:<s:password name= "password" label= "password" ></s:password><br/> repassword:<s:password name= "repassword" label= "repassword" ></s:password><br/> age:<s:textfield name= "age" label= "age" ></s:textfield><br/> birthday:<s:textfield name= "birthday" label= "brithday" ></s:textfield><br/> graduate:<s:textfield name= "graduate" label= "graduate" ></s:textfield><br/> <s:submit value= "submit" ></s:submit> </s:form> |
5、為了安全性,struts在沒有定義method的時候,是按照post方式提交的,這樣子比較安全
6、如果輸入的值不符合法,比如說age是int類型的,輸入的為String類型,這個時候系統(tǒng)會判斷并往Field級別添加Invalid field value for field”age" 這個信息如下:
執(zhí)行流程:
1)首先進行類型轉(zhuǎn)換
2)然后進行輸入效驗(執(zhí)行validate方法)
3)如果在上述過程中出現(xiàn)了任何錯誤,都不會再去執(zhí)行execute方法,頁面會轉(zhuǎn)向struts.xml中該action的name為input的result所對應(yīng)的頁面。
8.ActionSupport類的addActionError()方法的實現(xiàn):首先創(chuàng)建一個ArrayList對象,然后將錯誤消息添加到該ArrayList對象中。
9、當(dāng)調(diào)用getActionErrors()方法返回Action級別的錯誤信息列表時,返回的實際上是集合的一個副本而不是集合本身,因此對集合副本調(diào)用clear()方法清除的依舊是副本中的元素而非原集合中的元素,此時原集合中的內(nèi)容沒有收到任何的影響。換句話說,Action級別的錯誤信息列表對開發(fā)者來說是可讀的,但不可寫
如果說要在validate后將錯誤信息刪除掉,讓其即使有錯誤信息也照樣去執(zhí)行execute方法,則可以調(diào)用this.clearAllActionErrors()或者this.clearAllFieldErrors()方法
10、FieldError級別的錯誤信息底層是通過LinkedHashMap實現(xiàn)的,該Map的key是String類型,value是List<String>類型,這就表示一個Field Name可以對應(yīng)多條錯誤信息,這些錯誤信息都放置在List<String>集合當(dāng)中。 從而達到同一個錯誤有多個錯誤信息
以上所述是小編給大家介紹的Struts中使用validate()輸入校驗方法詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!