使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类放在同一个包下,文件的取名格式为:ActionClassName-validation.xml,其中ActionClassName为action的简单类名,-validation为固定写法。如果Action类为edu.action.RegistAction,那么该文件的取名应为:RegistAction-validation.xml。下面是校验文件的模版:(取自struts-2.3.4.1\apps\struts2-mailreader\WEB-INF\src\java\mailreader2\Registration-validation.xml下)
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <!-- 支持国际化,key的值为资源文件中的key --> <message key="error.username.required"/> </field-validator> </field> <!-- 对复合类型属性的校验 --> <field name="user.fullName"> <field-validator type="requiredstring"> <message key="error.fullName.required"/> </field-validator> </field> <field name="user.fromAddress"> <field-validator type="requiredstring"> <message key="error.fromAddress.required"/> </field-validator> <field-validator type="email"> <message key="errors.email"/> </field-validator> </field> <field name="user.replyToAddress"> <field-validator type="email"> <message key="errors.email"/> </field-validator> </field> </validators>
2.基于XML配置方式实现对action的指定方法进行输入校验
当校验文件的取名为ActionClassName-validation.xml时,会对 action中的所有处理方法实施输入验证。如果你只需要对action中的某个action方法实施校验,那么,校验文件的取名应为:ActionClassName-ActionName-validation.xml,其中ActionName为struts.xml中action的名称。例如:在实际应用中,常有以下配置:
<package name="validate" namespace="/validate" extends="struts-default"> <action name="user_*" class="edu.action.RegistAction" method="{1}"> <result name="input" >/page/regist.jsp</result> <result name="message">/page/message.jsp</result> </action> </package>RegistAction中有以下两个处理方法:
(1)当为某个action提供了ActionClassName-validation.xml和ActionClassName-ActionName-validation.xml两种规则的校验文件时,系统按下面顺序寻找校验文件:(1)AconClassName-validation.xml (2)ActionClassName-ActionName-validation.xml
(2)系统寻找到第一个校验文件时还会继续搜索后面的校验文件,当搜索到所有校验文件时,会把校验文件里的所有校验规则汇总,然后全部应用于action方法的校验。如果两个校验文件中指定的校验规则冲突,则只使用后面文件中的校验规则。
(3)当action继承了另一个action,父类action的校验文件会先被搜索到。
假设UserAction继承BaseAction:
<action name="user" class="cn.itcast.action.UserAction" method="{1}">
</action>
问上面action,系统先搜索父类的校验文件:BaseAction-validation.xml, BaseAction-user-validation.xml,接着搜索子类的校验文件:UserAction-validation.xml, UserAction-user-validation.xml。应用于上面action的校验规则为这四个文件的总和。
例子:
struts-validate.xml
<constant name="struts.configuration.xml.reload" value="true"></constant> <package name="validate" namespace="/validate" extends="struts-default"> <action name="user_*" class="edu.action.RegistAction" method="{1}"> <result name="input" >/page/regist.jsp</result> <result name="message">/page/message.jsp</result> </action> </package>RegistAction.java
package edu.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class RegistAction extends ActionSupport{ private String userName; private String userTel; private Date birthday; private String message;//存放方法调用状况信息,则message.jsp显示此信息 public String getMessage() { return message; } public void setUserName(String userName) { this.userName = userName; } public void setUserTel(String userTel) { this.userTel = userTel; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String add() { this.message = "success in add()"; return "message"; } public String update(){ this.message = "success in update()"; return "message"; }regist.jsp
<body> <form action="${pageContext.request.contextPath}/validate/user_add.action" method="get"> UserName:<input type="text" name="userName"><font color="red"><s:fielderror fieldName="userName"/></font><br/> UserTel:<input type="text" name="userTel"><font color="red"><s:fielderror fieldName="userTel"/></font><br/> Birthday:<input type="text" name="birthday"><font color="red"><s:fielderror fieldName="birthday"/></font><br/> <input type="submit" value="regist"><br/> </form> </body>RegistAction-user_add-validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="userName"> <field-validator type="requiredstring"> <message>用户名不能为空!</message> </field-validator> </field> <field name="userTel"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>联系方式不能为空</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[^1[3458]\\d{9}$]]></param> <message>联系方式格式不对!</message> </field-validator> </field> </validators>
RegistAction-validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="birthday"> <field-validator type="requiredstring"> <message>生日不能为空!</message> </field-validator> </field> </validators>
表单提交路径改为http://localhost:8080/Struts2_05/validate/user_update.action后(即调用Action的update方法,则只会进行RegistAction-validation.xml中注册的校验)
关于手工编写代码实现输入校验请参见我的博客: 输入校验--手工编写代码实现