(转)struts Validator验证框架实例

使用Validator验证框架实现注册表彰的数据验证

1.建立注册页面:

Html代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"  
  3.     prefix="bean"%>  
  4. <%@ taglib uri="http://jakarta.apache.org/struts/tags-html"  
  5.     prefix="html"%>  
  6.   
  7. <html>  
  8.     <head>  
  9.         <title>JSP for DynaValidatorForm form</title>  
  10.     </head>  
  11.     <body>  
  12.         <center>  
  13.             <html:form action="/regist">  
  14.                 <table>  
  15.                     <tr>  
  16.                         <td>  
  17.                             <bean:message key="emp.username" />  
  18.                         </td>  
  19.                         <td>  
  20.                             <html:text property="username" />  
  21.                             <html:errors property="username" />  
  22.                         </td>  
  23.                     </tr>  
  24.                     <tr>  
  25.                         <td>  
  26.                             <bean:message key="emp.password" />  
  27.                         </td>  
  28.   
  29.                         <td>  
  30.                             <html:password property="password" />  
  31.                             <html:errors property="password" />  
  32.                         </td>  
  33.                     </tr>  
  34.                     <tr>  
  35.                         <td>  
  36.                             <bean:message key="emp.repassword" />  
  37.                         </td>  
  38.                         <td>  
  39.                             <html:password property="repassword" />  
  40.                             <html:errors property="repassword" />  
  41.                         </td>  
  42.                     </tr>  
  43.                     <tr>  
  44.                         <td>  
  45.                             <bean:message key="emp.birthday" />  
  46.                         </td>  
  47.                         <td>  
  48.                             <html:text property="birthday" />  
  49.                             <html:errors property="birthday" />  
  50.                         </td>  
  51.                     </tr>  
  52.                     <tr>  
  53.                         <td>  
  54.                             <bean:message key="emp.email" />  
  55.                         </td>  
  56.                         <td>  
  57.                             <html:text property="email" />  
  58.                             <html:errors property="email" />  
  59.                         </td>  
  60.                     </tr>  
  61.                     <tr>  
  62.                         <td>  
  63.                             <bean:message key="emp.mobile" />  
  64.                         </td>  
  65.                         <td>  
  66.                             <html:text property="mobile" />  
  67.                             <html:errors property="mobile" />  
  68.                         </td>  
  69.                     </tr>  
  70.                     <tr>  
  71.                         <td>  
  72.                             <html:submit value="注册新用户" />  
  73.                         </td>  
  74.                         <td>  
  75.                             <html:cancel value="重置" />  
  76.                         </td>  
  77.                     </tr>  
  78.                 </table>  
  79.             </html:form>  
  80.         </center>  
  81.     </body>  
  82. </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代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
  3. <struts-config>  
  4.     <data-sources />  
  5.     <form-beans>  
  6.         <form-bean name="registForm"  
  7.             type="org.apache.struts.validator.DynaValidatorForm">  
  8.             <form-property name="password" type="java.lang.String" />  
  9.             <form-property name="mobile" type="java.lang.String" />  
  10.             <form-property name="username" type="java.lang.String" />  
  11.             <form-property name="email" type="java.lang.String" />  
  12.             <form-property name="repassword" type="java.lang.String" />  
  13.             <form-property name="birthday" type="java.lang.String" />  
  14.         </form-bean>  
  15.     </form-beans>  
  16.     <global-exceptions />  
  17.     <global-forwards />  
  18.     <action-mappings>  
  19.         <action attribute="registForm" input="/regist.jsp"  
  20.             validate="true" name="registForm" path="/regist" scope="request"  
  21.             type="com.sun.demo.action.RegistAction" />  
  22.     </action-mappings>  
  23.     <message-resources parameter="com.sun.demo.ApplicationResources" />  
  24.     <plug-in className="org.apache.struts.validator.ValidatorPlugIn">  
  25.         <set-property property="pathnames"  
  26.             value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />  
  27.         <set-property property="stopOnFirstError" value="false" />  
  28.     </plug-in>  
  29. </struts-config>  

 具体规则有:

1).将相应<action>元素的validate属性值设为true.

2).指定具体要验证的内容以及验证的规则:因为我们通常可以只需对部分表单数据进行指定的验证处理,这些信息分别保存在validation.xml和validator-rules.xml两个验证配置文件中

 

3.创建和部署验证规则及验证内容文件。

源文件:validator.xml

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE form-validation PUBLIC   
  3.           "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"   
  4.           "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">  
  5. <form-validation>  
  6.     <formset>  
  7.         <form name="registForm">  
  8.             <field property="username" depends="required,,minlength">  
  9.                 <arg0 key="emp.username" />  
  10.                 <arg1 key="${var:minlength}" resource="false" />  
  11.                 <var>  
  12.                     <var-name>minlength</var-name>  
  13.                     <var-value>6</var-value>  
  14.                 </var>  
  15.             </field>  
  16.             <field property="password" depends="required,minlength">  
  17.                 <arg0 key="emp.password" />  
  18.                 <arg1 key="${var:minlength}" resource="false" />  
  19.                 <var>  
  20.                     <var-name>minlength</var-name>  
  21.                     <var-value>6</var-value>  
  22.                 </var>  
  23.             </field>  
  24.             <field property="repassword" depends="required,validwhen">  
  25.                 <arg0 key="emp.repassword" />  
  26.                 <arg1 key="emp.password" />  
  27.                 <var>  
  28.                     <var-name>test</var-name>  
  29.                     <var-value>  
  30.                         ((password == *this*) and (*this* != null))   
  31.                     </var-value>  
  32.                 </var>  
  33.             </field>  
  34.             <field property="birthday" depends="required,date">  
  35.                 <arg0 key="emp.birthday" />  
  36.                 <var>  
  37.                     <var-name>datePattern</var-name>  
  38.                     <var-value>yyyy-MM-dd</var-value>  
  39.                 </var>  
  40.             </field>  
  41.             <field property="email" depends="required,email">  
  42.                 <arg0 key="emp.email" />  
  43.             </field>  
  44.             <field property="mobile" depends="required,mask">  
  45.                 <arg0 key="emp.mobile" />  
  46.                 <var>  
  47.                     <var-name>mask</var-name>  
  48.                     <var-value>(13|15)/d{9}</var-value>  
  49.                 </var>  
  50.             </field>  
  51.         </form>  
  52.     </formset>  
  53. </form-validation>  

注意,在这里的validwhen由于它的错误信息默认对应的是errors.required,我们将之修改为errors.validwhen

即在validator-rules.xml文件中修改如下代码:

Xml代码
  1. <validator name="validwhen"  
  2.           msg="errors.validwhen"  

 

4.创建资源包属性文件

源文件:ApplicationResources.properties

Java代码
  1. errors.required={0} /u4e0d/u80fd/u4e3a/u7a7a   
  2. errors.minlength={0} /u4e0d/u80fd/u5c11/u4e8e {1} /u4e2a/u5b57/u7b26   
  3. errors.maxlength={0} /u4e0d/u80fd/u5927/u4e8e {1} /u4e2a/u5b57/u7b26   
  4. errors.invalid={0} /u662f/u975e/u6cd5/u7684   
  5. errors.validwhen={0} /u4e0e {1} /u4e0d/u5339/u914d   
  6.   
  7. errors.date={0} /u4e0d/u7b26/u5408/u65e5/u671f/u683c/u5f0f   
  8. errors.email={0} /u4e0d/u662f/u5408/u6cd5/u7684/u7535/u5b50/u90ae/u7bb1   
  9.   
  10. emp.username=/u7528/u6237/u540d   
  11. emp.password=/u5bc6/u7801   
  12. emp.repassword=/u786e/u8ba4/u5bc6/u7801   
  13. emp.birthday=/u51fa/u751f/u65e5/u671f   
  14. emp.email=/u7535/u5b50/u90ae/u7bb1   
  15. 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

 其对应的中文原文是:

Xml代码
  1. errors.required={0} 不能为空   
  2. errors.minlength={0} 不能少于 {1} 个字符   
  3. errors.maxlength={0} 不能大于 {1} 个字符   
  4. errors.invalid={0} 是非法的   
  5. errors.validwhen={0} 与 {2} 不匹配   
  6.   
  7. errors.date={0} 不符合日期格式   
  8. errors.email={0} 不是合法的电子邮箱   
  9.   
  10. emp.username=用户名   
  11. emp.password=密码   
  12. emp.repassword=确认密码   
  13. emp.birthday=出生日期   
  14. emp.email=电子邮箱   
  15. emp.mobile=手机号  

 5.创建处理器Action Bean类

Java代码
  1. package com.sun.demo.action;   
  2.   
  3. import javax.servlet.http.HttpServletRequest;   
  4. import javax.servlet.http.HttpServletResponse;   
  5. import org.apache.struts.action.Action;   
  6. import org.apache.struts.action.ActionForm;   
  7. import org.apache.struts.action.ActionForward;   
  8. import org.apache.struts.action.ActionMapping;   
  9. import org.apache.struts.validator.DynaValidatorForm;   
  10.   
  11. public class RegistAction extends Action {   
  12.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  13.             HttpServletRequest request, HttpServletResponse response) {   
  14.         DynaValidatorForm registForm = (DynaValidatorForm) form;   
  15.         String username = (String) registForm.get("username");   
  16.         String password = (String) registForm.get("password");   
  17.         return null;   
  18.     }   
  19. }  
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)页面

Java代码
  1. <html:javascript formName="registForm"/>    
  2. <html:form action="regist.do" οnsubmit="return validateRegistForm(this)"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值