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