一、校验器的配置风格
1、字段校验器:
<field name="被校验的字段">
<field-validator type="校验器名">
<!--此处需要为不同校验器指定数量不等的校验规则-->
<param name="参数名">参数值</param>
....................
<!--校验失败后的提示信息,其中key指定国际化信息的key-->
<message>校验失败后的提示信息</message>
<!--校验失败后的提示信息:建议用getText("I18Nkey"),否则可能出现Freemarker template Error-->
</field-vallidator>
<!-- 如果校验字段满足多个规则,下面可以配置多个校验器-->
</field>
2、非字段校验器(本实例所使用):
<validator type="校验器名">
<param name="fieldName">需要被校验的字段</param>
<!--此处需要为不同校验器指定数量不等的校验规则-->
<param name="参数名">参数值</param>
<message>校验失败后的提示信息</message>
</validator>
两种配置只是风格上的不同,没有本质区别。
二、实例
本注册项目中使用了4个内置校验器。项目只有一个注册页面(register2.jsp),对应的业务控制器(Register2Action.java)。如果输入的数据经内置验证器验证成功,跳转到success1.jsp。
实例步骤如下:
注意:
Register2Action.java和Register2Action-validation.xml在同一个包validatorAction内
register2.jsp和success1.jsp在WebContent下(我用的是Eclipse)同一个文件夹validate内
1、web.xml配置FilterDispatcher
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <!-- 配置Struts2核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 配置Struts2核心Filter的实现类 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 配置Filter拦截的URL --> <filter-mapping> <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2、编写视图组件(JSP页面)
(1)注册页面(register2.jsp)
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户注册页面</title> </head> <body> <center> 请输入信息: <hr> <s:form action="register2" method="post"> <table border="1"> <tr> <td> <s:textfield name="userName" label="姓名" size="16" /> </td> </tr> <tr> <td> <s:password name="userPassword" label="密码" size="18" /> </td> </tr> <tr> <td> <s:password name="ruserPassword" label="再次输入密码" size="18" /> </td> </tr> <tr> <td> <s:textfield name="userAge" label="年龄" size="16" /> </td> </tr> <tr> <td> <s:textfield name="userTelephone" label="电话" size="16" /> </td> </tr> <tr> <td> <s:textfield name="userEmail" label="邮箱" size="16" /> </td> </tr> <tr> <td> <s:submit value="提交" /> </td> </tr> </table> </s:form> </center> </body> </html>
(2)验证成功页面(success1.jsp)
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>校验成功</title> </head> <body> 校验通过,用户信息如下: <hr> 姓名:<s:property value="userName" /> <br> 密码:<s:property value="userPassword" /> <br> 年龄:<s:property value="userAge" /> <br> 电话:<s:property value="userTelephone" /> <br> 邮箱:<s:property value="userEmail" /> </body> </html>
3、编写业务控制器(Register2Action.java)
package validatorAction; import com.opensymphony.xwork2.ActionSupport; public class Register2Action extends ActionSupport{ private String userName; private String userPassword; private String ruserPassword; private int userTelephone; private int userAge; private String userEmail; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getRuserPassword() { return ruserPassword; } public void setRuserPassword(String ruserPassword) { this.ruserPassword = ruserPassword; } public int getUserTelephone() { return userTelephone; } public void setUserTelephone(int userTelephone) { this.userTelephone = userTelephone; } public int getUserAge() { return userAge; } public void setUserAge(int userAge) { this.userAge = userAge; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String execute(){ return "success"; } }
4、配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="intercept" extends="struts-default"> <action name="register2" class="validatorAction.Register2Action"> <result name="input">/validate/register2.jsp</result> <result name="success">/validate/success1.jsp</result> </action> </package> </struts>
5、内置验证器的验证文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <!-- 字符串长度校验器 ,校验userName属性--> <validator type="stringlength"> <param name="fieldName">userName</param> <param name="maxLength">16</param> <param name="minLength">6</param> <message>姓名长度为${minLength}到${maxLength}个字符!</message> </validator> <!-- 字符串长度校验器 ,校验userPassword属性--> <validator type="stringlength"> <param name="fieldName">userPassword</param> <param name="maxLength">16</param> <param name="minLength">6</param> <message>密码长度为${minLength}到${maxLength}个字符!</message> </validator> <!-- 字段表达式校验器--> <validator type="fieldexpression"> <param name="fieldName">userPassword</param> <param name="expression"> <!-- 验证两次输入的密码是否相同 --> <![CDATA[userPassword==ruserPassword]]> </param> <message>两次密码不一致!</message> </validator> <!-- 整数校验器--> <validator type="int"> <param name="fieldName">userAge</param> <param name="min">1</param> <param name="max">130</param> <message>年龄必须在${min}到${max}之间!</message> </validator> <!-- 整数校验器 --> <validator type="int"> <param name="fieldName">userTelephone</param> <param name="min">22222222</param> <param name="max">99999999</param> <message>电话必须在${min}到${max}之间的八位号码!</message> </validator> <!-- 邮件地址校验器--> <validator type="email"> <param name="fieldName">userEmail</param> <message>请使用正确的邮件格式!</message> </validator> </validators>
如果验证失败:
检查stringlength等属性的大小写,还有字段是否和前面匹配;
查看struts-2.3.28.1\src\xwork-core\src\main\resources\xwork-validator-1.0.3.dtd文件,拷贝前4行代码,因版本而异。