struts2學習筆記--使用Validator校驗數據
來自: http://www.cnblogs.com/fingerboy/p/5189742.html
我們在進行一些操作是需要對用戶的輸入數據進行驗證,比如網站的注冊,需要對各個數據項進行數據校驗,Struts2提供了一些默認的校驗器,比如數字的檢測,郵箱的檢測,字符串長度的檢測等等.
-
常用的Validator
校驗器 | 作用 |
required | 必填校驗器,要求字段必須有值 |
requiredstring | 必填字符串校驗器,要求必須有值且長度大于0,即不能是空字符串。默認會去掉字符串前后空格 參數fieldName:該參數指定校驗的字段名稱,如果是字段校驗,則不用指定該參數 參數trim:該參數為可選參數,用于指定是否在校驗之前對字符串進行整理。 |
stringlength | 字符串長度校驗器,用于檢驗字段中字符串長度是否在指定的范圍 參數 maxLength:用于指定最大字符串長度,該參數為可選 參數 minLength:用于指定最小字符串長度,該參數為可選 |
int | 整數校驗器,可以配置整數在指定的范圍內 參數 min:指定字段值的最小值,該參數為可選 參數 max:指定字段值的最大值,該參數為可選 |
date | 日期校驗器,可以配置日期在指定的范圍內 參數 min:指定字段日期值的最小值,該參數為可選 參數 max:指定字段日期值的最大值,該參數為可選 |
郵件地址校驗器,要求被檢查的字段如果非空,則必須是合法的郵件地址。 |
|
regex | 檢查是否能匹配到正則表達式,參數為regex |
使用struts2的驗證框架的要求:
在對應的action的包下添加一個驗證框架的配置文件,文件名稱為Action類名-validation.xml.如果Action中有多個方法,則一般使用Action類名-Action別名-validation.xml.例如LoginAction-addUser-validation.xml.
特別需要注意的是:支持校驗的Action必須實現Validateable接口,一般繼承ActionSupport類就可以了.
下面寫一個簡單的用戶注冊的demo來使用一下Validator,在web項目下新建一個注冊的jsp文件,名為regist.jsp,引入ognl標簽庫,body部分代碼為:
<body> <s:fielderror></s:fielderror> <s:form action="user/regist.action" validate="true"> 用戶名:<input type="text" name="user.name"/><br> 密碼:<input type="password" name="user.password"/><br> 出生日期:<input type="text" name="user.date"/><br> 電子郵箱:<input type="text" name="user.email"/><br> <input type="submit" value="注冊"/> </s:form> </body>
entity包下新建一個user實體類,代碼省略.
action包下新建一個RegistAction,繼承ActionSupport類,代碼省略.
action包下新建一個RegistAction-validation.xml,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "; <validators> <field name="user.name"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用戶名不能為空</message> </field-validator> <field-validator type="stringlength"> <param name="trim">true</param> <param name="maxLength">10</param> <param name="minLength">4</param> <message>用戶名長度必須介于4到10之間</message> </field-validator></field> <field name="user.birthday"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2016-01-01</param> <message>日期不滿足要求</message> </field-validator> </field> <field name="user.email"> <field-validator type="email"> <param name="trim">true</param> <message>郵箱格式不滿足要求</message> </field-validator> </field> </validators></pre>
struts.xml內配置action:
<package name="user" namespace="/user" extends="struts-default"> <action name="regist" class="com.wang.action.RegistAction" > <result>/index.jsp</result> <result name="input">/register.jsp</result> </action> </package>如果檢驗失敗,會轉到input頁面顯示錯誤信息,因此action配置中必須要有一個名為input的jsp頁面.運行之后,如果輸入不符合要求的數據則會在瀏覽器提示出來.
我在寫這個demo時碰到一個問題, 因為使用了ognl標簽,在輸入url訪問regist.jsp頁面時,報了一個錯誤 :
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
后來 將web.xml的url-pattern中的*.action改為/* 就解決了.據說還有一種方式是不改變上面配置,只需要 增加一個url-pattern為*.jsp即可 ,待檢驗.
</div>