网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

如何扩充struts验证框架,进行多表单页面的验证

http://www.diybl.com/ 2007-2-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】


struts的validator的客户端验证,不能进行多表单页面的验证,原因是由<html:script>标签生成的javascipt是根据每个表单,生成一段代码。例如:

<html:javascript formName="searchSgbySjForm" dynamicJavascript="true" staticJavascript="false"/>生成     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 <code>ValidatorPlugIn</code> * 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;

            }

文章整理:DIY部落 http://www.diybl.com (本站)   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言