浏览 2297 次
锁定老帖子 主题:struts验证两个字段是否相等
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-10-26
最后修改:2011-01-21
前台页面: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <html> <head> <title>JSP for RegisterForm form</title> </head> <body> <html:javascript formName="registerForm"/> <html:errors/> <html:form action="/register" onsubmit="return validateRegisterForm(this);"> password : <html:password property="password"/><html:errors property="password"/><br/> password1 : <html:password property="password1"/><html:errors property="password1"/><br/> <html:submit/><html:cancel/> </html:form> </body> </html> 自定义的验证规则: <!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> <global> <validator name="identical" classname="com.hxb.validator.IdenticalField" method="validateIdentical" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" msg="errors.identical"/> </global> </form-validation> validation.xml文件: <!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="registerForm"> <field property="password" depends="required" > <arg0 key="form.password"/> </field> <field property="password1" depends="required,identical" > <arg0 key="form.password1"/> <arg1 key="form.password"/> <var> <var-name>firstProperty</var-name> <var-value>password</var-value> </var> </field> </form> </formset> </form-validation> 验证类: package com.hxb.validator; import org.apache.commons.validator.GenericValidator; import org.apache.commons.validator.util.ValidatorUtils; import org.apache.struts.action.ActionMessages; import org.apache.struts.validator.Resources; public class IdenticalField{ public static boolean validateIdentical(java.lang.Object bean, org.apache.commons.validator.ValidatorAction va, org.apache.commons.validator.Field field, ActionMessages errors, org.apache.commons.validator.Validator validator, javax.servlet.http.HttpServletRequest request){ String value2 = ValidatorUtils.getValueAsString(bean, field.getProperty()); if (!GenericValidator.isBlankOrNull(value2)) { if(value2.equals(ValidatorUtils.getValueAsString(bean, field.getVarValue("firstProperty")))){ return true; } } errors.add(field.getProperty(), Resources.getActionMessage(request, va, field)); return false; } } 资源文件: form.password=password form.password1=password1 errors.identical={0} and {1} are not identical. 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="registerForm" type="com.hxb.struts.form.RegisterForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="registerForm" input="/register.jsp" name="registerForm" path="/register" scope="request" type="com.hxb.struts.action.RegisterAction" validate="true"/> </action-mappings> <message-resources parameter="com.hxb.struts.ApplicationResources" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/custom-validator-rules.xml, /WEB-INF/validation.xml,/WEB-INF/validator-rules.xml"/> </plug-in> </struts-config> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |