如何扩充struts验证框架,进行多表单页面的验证
[作者]:
菩提树下的杨过 [来源]:互联网 [收录时间]:2007-8-1 20:15:10
struts的validator的客户端验证,不能进行多表单页面的验证,原因是由
标签生成的javascipt是根据每个表单,生成一段代码。例如:
生成 var bCancel = false;
function validateSearchSgbySjForm(form) { if (bCancel) return true; else return validateRequired(form) && validateDate(form); }
function required () { this.aa = new Array("sgfssjq", "事故发生时间起 不可为空.", new Function ("varName", "this.datePatternStrict='yyy-MM-dd'; return this[varName];")); this.ab = new Array("sgfssjz", "事故发生时间止 不可为空.", new Function ("varName", "this.datePatternStrict='yyy-MM-dd'; return this[varName];")); }
function DateValidations () { this.aa = new Array("sgfssjq", "事故发生时间起 不是有效的日期类型.", new Function ("varName", "this.datePatternStrict='yyy-MM-dd'; return this[varName];")); this.ab = new Array("sgfssjz", "事故发生时间止 不是有效的日期类型.", new Function ("varName", "this.datePatternStrict='yyy-MM-dd'; return this[varName];")); }
如果有多个的话required和DateValidations 都会重复的,而javascript是只认最后一个函数的。所以,会导致验证出错。
再写一个标签 ,主要根据原来的代码修改,代码如下:
/* * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/HTML/javascriptValidatorTag.java,v 1.52 2004/08/07 04:17:52 martinc Exp $ * $Revision: 1.52 $ * $Date: 2004/08/07 04:17:52 $ * * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package com.tmri.acd.tag;
import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;
import javax.servlet.JSP.JSPException;import javax.servlet.JSP.JSPWriter;import javax.servlet.JSP.PageContext;import javax.servlet.JSP.tagext.BodyTagSupport;
import org.apache.commons.validator.Field;import org.apache.commons.validator.Form;import org.apache.commons.validator.ValidatorAction;import org.apache.commons.validator.ValidatorResources;import org.apache.commons.validator.util.ValidatorUtils;import org.apache.commons.validator.Var;import org.apache.struts.Globals;import org.apache.struts.action.ActionMapping;import org.apache.struts.config.ModuleConfig;import com.tmri.acd.tag.TagUtils;import org.apache.struts.util.MessageResources;import org.apache.struts.validator.Resources;import org.apache.struts.validator.ValidatorPlugIn;import java.util.StringTokenizer;
/** * Custom tag that generates javascript for client side validation based * on the validation rules loaded by the ValidatorPlugIn * defined in the struts-config.XML file. * * @version $Revision: 1.52 $ $Date: 2004/08/07 04:17:52 $ * @since Struts 1.1 */public class javascriptValidatorTag extends BodyTagSupport {
/** * A Comparator to use when sorting ValidatorAction objects. */ private static final Comparator actionComparator = new Comparator() { public int compare(Object o1, Object o2) {
ValidatorAction va1 = (ValidatorAction) o1; ValidatorAction va2 = (ValidatorAction) o2;
if ((va1.getDepends() == null || va1.getDepends().length() == 0) && (va2.getDepends() == null || va2.getDepends().length() == 0)) { return 0;
} else if ( (va1.getDepends() != null && va1.getDepends().length() > 0) && (va2.getDepends() == null || va2.getDepends().length() == 0)) { return 1;
} else if ( (va1.getDepends() == null || va1.getDepends().length() == 0) && (va2.getDepends() != null && va2.getDepends().length() > 0)) { return -1;
} else { return va1.getDependencyList().size() - va2.getDependencyList().size(); } } };
/** * The start of the HTML comment hiding javascript from old browsers. * @since Struts 1.2 */ protected static final String HTML_BEGIN_COMMENT = "\n \n";
// ----------------------------------------------------------- Properties
/** * The servlet context attribute key for our resources.