struts2 validator驗證隨筆

環境:eclipse-jee-galileo-SR2-win32+apache-tomcat-6.0.18+struts-2.3.14 html

最近學習struts2框架,就在此隨筆謝謝,若有不足,請各位賜教。 java

1.工程出現下面錯誤: express

2013-6-2 20:28:15 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'loginRegister' in namespace: '/'. Form action defaulting to 'action' attribute's literal value. apache

首先檢查jsp中form標籤裏action和struts.xml裏配置的action名是否相同。若是名相同的話,確認是否在相應的namespace下。若是不在同一個域名下,就在form標籤里加上namespace="/xx"(xx對應struts.xml裏定義的namespace值),如未指定,struts2會默認從根命名空間"/" 搜索action。 tomcat

須要注意的一點是出現這樣的問題,有多是由於你在<s:form>標籤裏配置action時添加了.action,在<s:form>中,這個.action不須要由咱們手工添加,struts2會自動爲你完成這個工做,在jsp頁面查看源碼能夠看到。 框架

2.<s:reset>標籤在出現error回顯時,失效。 eclipse

<s:reset>標籤的含義是將<s:form>表單中的值設置成初始值,而不是單純的清空。這一點要注意,也是出現這個問題的根本所在,在出現error後,跳回輸入界面,<s:form>裏的各個標籤裏面都是有值的,因此你<s:reset>不能設置他們爲空,若是不走action,只能手動在<s:reset>標籤上寫js解決了。 jsp

3.使用<s:radio>標籤出現下面error ide

<s:radio list="#{0:'保密',1:'男',2:'女'}" value = "2" name= "sex" 
key = "login.register.sex"></s:radio>

org.apache.jasper.JasperException: tag 'radio', field 'list', name 'usex': The requested list key '#{0:'保密',1:'男',2:'女'}' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location] post

這個問題糾結了很長時間,這樣寫jsp不須要衝後臺獲得集合,怎麼還會報出這個message呢?查了各類資料都解決不了,其實仍是細節問題,我把別人的代碼拷來執行就沒有問題,怎麼運行這個就有問題了,仔細檢查下來,發現有一個全角的冒號在裏面...

另外若是下面這樣使用標籤,也會出現這樣的錯誤:

<s:radio list="sexList" listKey="key" value = "usex" listValue="value" name="usex" 
key = "login.register.sex"></s:radio>
那是由於沒有走action(validator沒有經過,返回界面),或者走了action卻沒有對這個集合進行初期化,直接解決的方法是初期 化list的工做放到prepare攔截器中,由於prepare是在validate攔截器以前執行,即實現prepareble接口。

public class LoginRegister extends ActionSupport implements Preparable {
...
@Override
public void prepare() throws Exception {
	List<LoginRegisterSexDto> sexList = new ArrayList<LoginRegisterSexDto>();
        ...
	ActionContext.getContext().put("sexList", sexList);
}


須要說明的是標籤裏的屬性listKey,listValue是dto裏的兩個屬性,list屬性對應的是這個dto的集合。若是想回顯選中的值,只需將value屬性的值設置成與name值同樣就能夠了,可是這樣在畫面初期化的時候就不會有值了,若是想在初期化選中某個值的話,只須要將value屬性的的值設置成對應的值就好了(value="3"),但這樣又不能回顯被選中的值了,如何二者兼顧了...

4.在struts2進行自定義的類型轉換時,將string裝換成int類型會出現下面的錯誤:

警告: Error setting expression 'uage' with value '[Ljava.lang.String;@4fe91e'
ognl.MethodFailedException: Method "setUage" failed for object action.LoginRegister@87e704 [java.lang.NoSuchMethodException: action.LoginRegister.setUage([Ljava.lang.String;)]

若是將int換成Integer就沒有問題了,不知是什麼緣由形成這樣的問題。

5.另外還遇到一些,尚未解決,路過的大神可要幫幫呀...

跑validator的時候,出現下面的問題:

5.1出現下面的警告。

2013-6-2 21:26:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Got result of null when trying to get Boolean.

5.2LoginRegister-validation.xml中的正規表達式沒有起做用,以下:

<field-validator type = "regex" short-circuit="true">
	<param name = "expression"><![CDATA[(\w{6,20})]]></param>
	<message>${getText("name.rule")}</message>
</field-validator>
整個 LoginRegister-validation.xml文件以下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC 
"-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name = "uname">
	<field-validator type = "requiredstring" short-circuit="true">
		<message key = "name.required"></message>
	</field-validator>
	
	<field-validator type = "regex" short-circuit="true">
		<param name = "expression"><![CDATA[(\w{6,20})]]></param>
		<message>${getText("name.rule")}</message>
	</field-validator>
</field>

<field name = "upassword">
	<field-validator type = "requiredstring" short-circuit="true">
		<message>${getText("password.required")}</message>
	</field-validator>
	
	<field-validator type = "regex" short-circuit="true">
		<param name = "expression"><![CDATA[(\w{6,20})]]></param>
		<message>${getText("password.rule")}</message>
	</field-validator>
</field>

<field name = "repassword">
	<field-validator type = "requiredstring" short-circuit="true">
		<message>${getText("repassword.required")}</message>
	</field-validator>
	<field-validator type = "regex" short-circuit="true">
		<param name = "expression"><![CDATA[(\w{6,20})]]></param>
		<param name = "min">6</param>
		<param name = "max">20</param>
		<message>${getText("repassword.rule")}</message>
	</field-validator>
	<field-validator type = "fieldexpression" short-circuit="true">
		<param name = "expression"><![CDATA[repassword==upassword]]></param>
		<message>${getText("repassword.equal")}</message>
	</field-validator>
</field>

<field name = "uage">
	<field-validator type = "int">
		<param name = "min">0</param>
		<param name = "max">120</param>
		<message>${getText("age.rule")}</message>
	</field-validator>
</field>

<field name = "uemail">
	<field-validator type = "email">
		<message>${getText("email.rule")}</message>
	</field-validator>
</field>

<field name = "uphone">
	<field-validator type = "regex">
		<param name = "expression"><![CDATA[^1{358}\d(9)$]]></param>
		<message key ="phone.rule"></message>
	</field-validator>
</field>
</validators>

資源文件:messageResource_zh_CN.properties

age.rule=年齡必須在${min}至${max}之間
birth.rule=出生日期必須在1900-01-01至2012-01-01之間
email.rule=請輸入有效的電子郵箱格式
password.required=必須輸入密碼
password.rule=密碼長度必須在6至20之間
repassword.required=必須輸入確認密碼
repassword.equal=密碼與確認密碼必須一致
repassword.rule=確認密碼格式必須在6至20之間
name.required=必須輸入用戶名
name.rule=用戶名的長度必須在6至20之間
phone.rule=電話號碼爲13位

參考資料:

http://80eye.diandian.com/post/2010-05-25/17206015

http://bbs.csdn.net/topics/110118066

http://www.blogjava.net/landor2004/archive/2009/04/11/265016.html

http://blog.csdn.net/greencacti/article/details/7170086

http://wenku.baidu.com/view/e65397acd1f34693daef3ec6.html

討論帖:

http://www.oschina.net/question/1050450_113121

感謝熱心的前輩!!本文若有不正,請指出。

以上。

相關文章
相關標籤/搜索