新聞中心
Struts2驗證比起Struts1驗證框架來,好用多了,使程序更加清晰易讀,充分利用了配置文件的作用,也算是解耦的表現(xiàn)吧.

核心代碼如下:
1.用戶注冊頁面register.jsp
< ?xml:namespace prefix = s />< s:fielderror>< /s:fielderror>
< /FONT>
< !-- 讀取顯示提示信息 -->
< TABLE>
用戶名:
密碼:
< INPUT type=password name=user.password>
確認密碼:
< INPUT type=password name=user.rePassword>
年齡:
< INPUT name=user.age>
生日:
< INPUT name=user.birthday>
2.注冊成功歡迎頁面welcome.jsp
3.注冊處理action RegisterAction
4. 用戶Bean User.java
5.配置驗證文件RegisterAction-validation.xml
< VALIDATORS>
< !-- 驗證字符串不能為空 -->
< FIELD-VALIDATOR type="requiredstring">
< !-- 去空格 -->
< PARAM name="trim">true
< !-- 錯誤提示信息 -->
< MESSAGE>用戶名不能為空
< /FIELD-VALIDATOR>
< !-- 驗證字符串長度 -->
< FIELD-VALIDATOR type="stringlength">
< PARAM name="minLength">2< /PARAM>
20
< MESSAGE>用戶名長度應(yīng)在2到18個字符間
< FIELD-VALIDATOR type="requiredstring">
true
< MESSAGE>密碼不能為空
< /FIELD-VALIDATOR>
< FIELD-VALIDATOR type="stringlength">
< PARAM name="minLength">6< /PARAM>
18
密碼長度應(yīng)在6到18個字符之間< /MESSAGE>
< /FIELD-VALIDATOR>
< /FIELD>
< FIELD-VALIDATOR type="int">
< PARAM name="min">1
< PARAM name="max">150
< MESSAGE>年齡應(yīng)在1到150之間
< /FIELD-VALIDATOR>
< !-- 驗證字符串為日期類型 -->
< FIELD-VALIDATOR type="date">
< PARAM name="min">1900-01-01
< PARAM name="max">2008-10-16< /PARAM>
< MESSAGE>出生日期應(yīng)在1900-01-01到2008-10-16< /MESSAGE>
< /FIELD>
< /VALIDATORS>
6.struts2框架默認加載的配置文件struts.xml
7.web服務(wù)器啟動時加載Struts 配置文件 web.xml
至此,完畢.不過需要注意:
1.配置Struts2驗證xml文檔的名字有講究:格式為:Action名字-validation.xml;
2.驗證文檔里的 中的type類型要和VO中的User屬性類型一致,否則會報類型轉(zhuǎn)換錯誤
【編輯推薦】
- 在Eclipse中開發(fā)struts應(yīng)用程序
- 手把手教你在Eclipse中配置開發(fā)Struts
- Eclipse下開發(fā)struts完整解決亂碼問題
- Struts相關(guān)背景介紹
- 使用Easy Struts for Eclipse開發(fā)Struts
本文題目:淺談Struts2驗證框架及用戶注冊
鏈接地址:http://m.fisionsoft.com.cn/article/cdcedoo.html


咨詢
建站咨詢
