/*
 * $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 org.apache.struts.taglib.html;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 org.apache.struts.taglib.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;            } 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();
            }
        }
    };

解决方案 »

  1.   

    /**
         * The start of the HTML comment hiding JavaScript from old browsers.
         * @since Struts 1.2
         */
        protected static final String HTML_BEGIN_COMMENT = "\n<!-- Begin \n";    /**
         * The end of the HTML comment hiding JavaScript from old browsers.
         * @since Struts 1.2
         */
        protected static final String HTML_END_COMMENT = "//End --> \n";    // ----------------------------------------------------------- Properties    /**
         * The servlet context attribute key for our resources.
         */
        protected String bundle = Globals.MESSAGES_KEY;    /**
         * The default locale on our server.
         * @deprecated This variable is no longer used.
         */
        protected static Locale defaultLocale = Locale.getDefault();    /**
         * The name of the form that corresponds with the action name
         * in struts-config.xml. Specifying a form name places a
         * &lt;script&gt; &lt;/script&gt; around the javascript.
         */
        protected String formName = null;
        
        /**
         * formName is used for both Javascript and non-javascript validations.
         * For the javascript validations, there is the possibility that we will
         * be rewriting the formName (if it is a ValidatorActionForm instead of just
         * a ValidatorForm) so we need another variable to hold the formName just for
         * javascript usage.
         */
        protected String jsFormName = null;    /**
         * The line ending string.
         */
        protected static String lineEnd = System.getProperty("line.separator");    /**
         * The current page number of a multi-part form.
         * Only valid when the formName attribute is set.
         */
        protected int page = 0;    /**
         * This will be used as is for the JavaScript validation method name if it has a value.  This is
         * the method name of the main JavaScript method that the form calls to perform validations.
         */
        protected String methodName = null;    /**
         * Include language attribute in the &lt;script&gt; element.  This property is
         * ignored in XHTML mode.
         * @since Struts 1.2
         */
        protected boolean scriptLanguage = true;    /**
         * The static JavaScript methods will only be printed if this is set to "true".
         */
        protected String staticJavascript = "true";    /**
         * The dynamic JavaScript objects will only be generated if this is set to "true".
         */
        protected String dynamicJavascript = "true";    /**
         * The src attribute for html script element (used to include an external script
         * resource). The src attribute is only recognized
         * when the formName attribute is specified.
         */
        protected String src = null;    /**
         * The JavaScript methods will enclosed with html comments if this is set to "true".
         */
        protected String htmlComment = "true";    /**
         * Hide JavaScript methods in a CDATA section for XHTML when "true".
         */
        protected String cdata = "true";    /**
         * Gets the key (form name) that will be used
         * to retrieve a set of validation rules to be
         * performed on the bean passed in for validation.
         */
        public String getFormName() {
            return formName;
        }    /**
         * Sets the key (form name) that will be used
         * to retrieve a set of validation rules to be
         * performed on the bean passed in for validation.
         * Specifying a form name places a
         * &lt;script&gt; &lt;/script&gt; tag around the javascript.
         */
        public void setFormName(String formName) {
            this.formName = formName;
        }
      

  2.   

    如果在jsp中用java定义一个整型数组,能不能在调用javascript函数时传过去,不是html中的倥件数组。
      

  3.   

    js还没学完,但至少用以笨方法可以实现,select就可以作为一个中介,实现数组的传递。
    ie4+中应该还有更方便的方法;