使用Validator验证框架实现注册表彰的数据验证
1.建立注册页面:
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
- prefix="bean"%>
- <%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
- prefix="html"%>
- <html>
- <head>
- <title>JSP for DynaValidatorForm form</title>
- </head>
- <body>
- <center>
- <html:form action="/regist">
- <table>
- <tr>
- <td>
- <bean:message key="emp.username" />
- </td>
- <td>
- <html:text property="username" />
- <html:errors property="username" />
- </td>
- </tr>
- <tr>
- <td>
- <bean:message key="emp.password" />
- </td>
- <td>
- <html:password property="password" />
- <html:errors property="password" />
- </td>
- </tr>
- <tr>
- <td>
- <bean:message key="emp.repassword" />
- </td>
- <td>
- <html:password property="repassword" />
- <html:errors property="repassword" />
- </td>
- </tr>
- <tr>
- <td>
- <bean:message key="emp.birthday" />
- </td>
- <td>
- <html:text property="birthday" />
- <html:errors property="birthday" />
- </td>
- </tr>
- <tr>
- <td>
- <bean:message key="emp.email" />
- </td>
- <td>
- <html:text property="email" />
- <html:errors property="email" />
- </td>
- </tr>
- <tr>
- <td>
- <bean:message key="emp.mobile" />
- </td>
- <td>
- <html:text property="mobile" />
- <html:errors property="mobile" />
- </td>
- </tr>
- <tr>
- <td>
- <html:submit value="注册新用户" />
- </td>
- <td>
- <html:cancel value="重置" />
- </td>
- </tr>
- </table>
- </html:form>
- </center>
- </body>
- </html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<html>
<head>
<title>JSP for DynaValidatorForm form</title>
</head>
<body>
<center>
<html:form action="/regist">
<table>
<tr>
<td>
<bean:message key="emp.username" />
</td>
<td>
<html:text property="username" />
<html:errors property="username" />
</td>
</tr>
<tr>
<td>
<bean:message key="emp.password" />
</td>
<td>
<html:password property="password" />
<html:errors property="password" />
</td>
</tr>
<tr>
<td>
<bean:message key="emp.repassword" />
</td>
<td>
<html:password property="repassword" />
<html:errors property="repassword" />
</td>
</tr>
<tr>
<td>
<bean:message key="emp.birthday" />
</td>
<td>
<html:text property="birthday" />
<html:errors property="birthday" />
</td>
</tr>
<tr>
<td>
<bean:message key="emp.email" />
</td>
<td>
<html:text property="email" />
<html:errors property="email" />
</td>
</tr>
<tr>
<td>
<bean:message key="emp.mobile" />
</td>
<td>
<html:text property="mobile" />
<html:errors property="mobile" />
</td>
</tr>
<tr>
<td>
<html:submit value="注册新用户" />
</td>
<td>
<html:cancel value="重置" />
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html>
2.修改Struts配置文件,配置各核心组件,引用要用到的资源包,标记库和Struts验证插件(PlugIn)
源文件:struts-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
- <struts-config>
- <data-sources />
- <form-beans>
- <form-bean name="registForm"
- type="org.apache.struts.validator.DynaValidatorForm">
- <form-property name="password" type="java.lang.String" />
- <form-property name="mobile" type="java.lang.String" />
- <form-property name="username" type="java.lang.String" />
- <form-property name="email" type="java.lang.String" />
- <form-property name="repassword" type="java.lang.String" />
- <form-property name="birthday" type="java.lang.String" />
- </form-bean>
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings>
- <action attribute="registForm" input="/regist.jsp"
- validate="true" name="registForm" path="/regist" scope="request"
- type="com.sun.demo.action.RegistAction" />
- </action-mappings>
- <message-resources parameter="com.sun.demo.ApplicationResources" />
- <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
- <set-property property="pathnames"
- value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
- <set-property property="stopOnFirstError" value="false" />
- </plug-in>
- </struts-config>
具体规则有:
1).将相应<action>元素的validate属性值设为true.
2).指定具体要验证的内容以及验证的规则:因为我们通常可以只需对部分表单数据进行指定的验证处理,这些信息分别保存在validation.xml和validator-rules.xml两个验证配置文件中
3.创建和部署验证规则及验证内容文件。
源文件:validator.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE form-validation PUBLIC
- "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
- "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
- <form-validation>
- <formset>
- <form name="registForm">
- <field property="username" depends="required,,minlength">
- <arg0 key="emp.username" />
- <arg1 key="${var:minlength}" resource="false" />
- <var>
- <var-name>minlength</var-name>
- <var-value>6</var-value>
- </var>
- </field>
- <field property="password" depends="required,minlength">
- <arg0 key="emp.password" />
- <arg1 key="${var:minlength}" resource="false" />
- <var>
- <var-name>minlength</var-name>
- <var-value>6</var-value>
- </var>
- </field>
- <field property="repassword" depends="required,validwhen">
- <arg0 key="emp.repassword" />
- <arg1 key="emp.password" />
- <var>
- <var-name>test</var-name>
- <var-value>
- ((password == *this*) and (*this* != null))
- </var-value>
- </var>
- </field>
- <field property="birthday" depends="required,date">
- <arg0 key="emp.birthday" />
- <var>
- <var-name>datePattern</var-name>
- <var-value>yyyy-MM-dd</var-value>
- </var>
- </field>
- <field property="email" depends="required,email">
- <arg0 key="emp.email" />
- </field>
- <field property="mobile" depends="required,mask">
- <arg0 key="emp.mobile" />
- <var>
- <var-name>mask</var-name>
- <var-value>(13|15)/d{9}</var-value>
- </var>
- </field>
- </form>
- </formset>
- </form-validation>
注意,在这里的validwhen由于它的错误信息默认对应的是errors.required,我们将之修改为errors.validwhen
即在validator-rules.xml文件中修改如下代码:
- <validator name="validwhen"
- msg="errors.validwhen"
4.创建资源包属性文件
源文件:ApplicationResources.properties
- errors.required={0} /u4e0d/u80fd/u4e3a/u7a7a
- errors.minlength={0} /u4e0d/u80fd/u5c11/u4e8e {1} /u4e2a/u5b57/u7b26
- errors.maxlength={0} /u4e0d/u80fd/u5927/u4e8e {1} /u4e2a/u5b57/u7b26
- errors.invalid={0} /u662f/u975e/u6cd5/u7684
- errors.validwhen={0} /u4e0e {1} /u4e0d/u5339/u914d
- errors.date={0} /u4e0d/u7b26/u5408/u65e5/u671f/u683c/u5f0f
- errors.email={0} /u4e0d/u662f/u5408/u6cd5/u7684/u7535/u5b50/u90ae/u7bb1
- emp.username=/u7528/u6237/u540d
- emp.password=/u5bc6/u7801
- emp.repassword=/u786e/u8ba4/u5bc6/u7801
- emp.birthday=/u51fa/u751f/u65e5/u671f
- emp.email=/u7535/u5b50/u90ae/u7bb1
- emp.mobile=/u624b/u673a/u53f7
errors.required={0} /u4e0d/u80fd/u4e3a/u7a7a
errors.minlength={0} /u4e0d/u80fd/u5c11/u4e8e {1} /u4e2a/u5b57/u7b26
errors.maxlength={0} /u4e0d/u80fd/u5927/u4e8e {1} /u4e2a/u5b57/u7b26
errors.invalid={0} /u662f/u975e/u6cd5/u7684
errors.validwhen={0} /u4e0e {1} /u4e0d/u5339/u914d
errors.date={0} /u4e0d/u7b26/u5408/u65e5/u671f/u683c/u5f0f
errors.email={0} /u4e0d/u662f/u5408/u6cd5/u7684/u7535/u5b50/u90ae/u7bb1
emp.username=/u7528/u6237/u540d
emp.password=/u5bc6/u7801
emp.repassword=/u786e/u8ba4/u5bc6/u7801
emp.birthday=/u51fa/u751f/u65e5/u671f
emp.email=/u7535/u5b50/u90ae/u7bb1
emp.mobile=/u624b/u673a/u53f7
其对应的中文原文是:
- errors.required={0} 不能为空
- errors.minlength={0} 不能少于 {1} 个字符
- errors.maxlength={0} 不能大于 {1} 个字符
- errors.invalid={0} 是非法的
- errors.validwhen={0} 与 {2} 不匹配
- errors.date={0} 不符合日期格式
- errors.email={0} 不是合法的电子邮箱
- emp.username=用户名
- emp.password=密码
- emp.repassword=确认密码
- emp.birthday=出生日期
- emp.email=电子邮箱
- emp.mobile=手机号
5.创建处理器Action Bean类
- package com.sun.demo.action;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.validator.DynaValidatorForm;
- public class RegistAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- DynaValidatorForm registForm = (DynaValidatorForm) form;
- String username = (String) registForm.get("username");
- String password = (String) registForm.get("password");
- return null;
- }
- }
package com.sun.demo.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
public class RegistAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorForm registForm = (DynaValidatorForm) form;
String username = (String) registForm.get("username");
String password = (String) registForm.get("password");
return null;
}
}
6..validator框架客户端验证(javascript)页面
- <html:javascript formName="registForm"/>
- <html:form action="regist.do" οnsubmit="return validateRegistForm(this)">