⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynaactionform.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/DynaActionForm.java,v 1.16 2004/08/10 02:47:39 niallp Exp $
 * $Revision: 1.16 $
 * $Date: 2004/08/10 02:47:39 $
 *
 * Copyright 2000-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.action;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaClass;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.FormPropertyConfig;


/**
 * <p>Specialized subclass of <code>ActionForm</code> that allows the creation
 * of form beans with dynamic sets of properties, without requiring the
 * developer to create a Java class for each type of form bean.</p>
 *
 * <p><strong>USAGE NOTE</strong> - Since Struts 1.1, the
 * <code>reset</code> method no longer initializes property values to those
 * specified in <code>&lt;form-property&gt;</code> elements in the Struts
 * module configuration file.  If you wish to utilize that behavior, the
 * simplest solution is to subclass <code>DynaActionForm</code> and call
 * the <code>initialize</code> method inside it.</p>
 *
 * @version $Revision: 1.16 $ $Date: 2004/08/10 02:47:39 $
 * @since Struts 1.1
 */
public class DynaActionForm extends ActionForm implements DynaBean {


    // ----------------------------------------------------- Instance Variables


    /**
     * <p>The <code>DynaActionFormClass</code> with which we are associated.
     * </p>
     */
    protected DynaActionFormClass dynaClass = null;


    /**
     * <p>The set of property values for this <code>DynaActionForm</code>,
     * keyed by property name.</p>
     */
    protected HashMap dynaValues = new HashMap();


    // ----------------------------------------------------- ActionForm Methods


    /**
     * <p>Initialize all bean properties to their initial values, as specified
     * in the {@link FormPropertyConfig} elements associated with the
     * definition of this <code>DynaActionForm</code>.</p>
     *
     * @param mapping The mapping used to select this instance
     */
    public void initialize(ActionMapping mapping) {

        String name = mapping.getName();
        if (name == null) {
            return;
        }
        FormBeanConfig config =
            mapping.getModuleConfig().findFormBeanConfig(name);
        if (config == null) {
            return;
        }

        initialize(config);
    }

    public void initialize(FormBeanConfig config) {

        FormPropertyConfig props[] = config.findFormPropertyConfigs();
        for (int i = 0; i < props.length; i++) {
            set(props[i].getName(), props[i].initial());
        }

    }


    // :FIXME: Is there any point in retaining these reset methods
    // since they now simply replicate the superclass behavior?

    /**
     * <p>Reset bean properties to their default state, as needed.
     * This method is called before the properties are repopulated by
     * the controller.</p>
     *
     * <p>The default implementation attempts to forward to the HTTP
     * version of this method.</p>
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(ActionMapping mapping, ServletRequest request) {
        super.reset(mapping,request);
    }


    /**
     * <p>Reset bean properties to their default state, as needed.  This method is
     * called before the properties are repopulated by the controller.</p>
     *
     * <p>The default implementation (since Struts 1.1) does nothing.
     * Subclasses may override this method to reset bean properties to
     * default values, or the <code>initialize</code> method may be used to
     * initialize property values to those provided in the form property
     * configuration information (which was the behavior of
     * this method in some release candidates).</p>
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        super.reset(mapping,request);
    }


    // ------------------------------------------------------- DynaBean Methods


    /**
     * <p>Indicates if the specified mapped property contain a value for the
     * specified key value.</p>
     *
     * @param name Name of the property to check
     * @param key Name of the key to check
     *
     * @exception IllegalArgumentException if there is no property
     *  of the specified name
     */
    public boolean contains(String name, String key) {

        Object value = dynaValues.get(name);
        if (value == null) {
            throw new NullPointerException
                ("No mapped value for '" + name + "(" + key + ")'");
        } else if (value instanceof Map) {
            return (((Map) value).containsKey(key));
        } else {
            throw new IllegalArgumentException
                ("Non-mapped property for '" + name + "(" + key + ")'");
        }

    }


    /**
     * <p>Return the value of a simple property with the specified name.</p>
     *
     * @param name Name of the property whose value is to be retrieved
     *
     * @exception IllegalArgumentException if there is no property
     *  of the specified name
     * @exception NullPointerException if the type specified for the
     *  property is invalid
     */
    public Object get(String name) {

        // Return any non-null value for the specified property
        Object value = dynaValues.get(name);
        if (value != null) {
            return (value);
        }

        // Return a null value for a non-primitive property
        Class type = getDynaProperty(name).getType();
        if (type == null) {
            throw new NullPointerException
                ("The type for property " + name + " is invalid");
        }
        if (!type.isPrimitive()) {
            return (value);
        }

        // Manufacture default values for primitive properties
        if (type == Boolean.TYPE) {
            return (Boolean.FALSE);
        } else if (type == Byte.TYPE) {
            return (new Byte((byte) 0));
        } else if (type == Character.TYPE) {
            return (new Character((char) 0));
        } else if (type == Double.TYPE) {
            return (new Double(0.0));
        } else if (type == Float.TYPE) {
            return (new Float((float) 0.0));
        } else if (type == Integer.TYPE) {
            return (new Integer(0));
        } else if (type == Long.TYPE) {
            return (new Long(0));
        } else if (type == Short.TYPE) {
            return (new Short((short) 0));
        } else {
            return (null);
        }

    }


    /**
     * <p>Return the value of an indexed property with the specified name.
     * </p>
     *
     * @param name Name of the property whose value is to be retrieved
     * @param index Index of the value to be retrieved
     *
     * @exception IllegalArgumentException if there is no property
     *  of the specified name
     * @exception IllegalArgumentException if the specified property
     *  exists, but is not indexed
     * @exception IndexOutOfBoundsException if the specified index
     *  is outside the range of the underlying property
     * @exception NullPointerException if no array or List has been
     *  initialized for this property
     */
    public Object get(String name, int index) {

        Object value = dynaValues.get(name);
        if (value == null) {
            throw new NullPointerException
                ("No indexed value for '" + name + "[" + index + "]'");
        } else if (value.getClass().isArray()) {
            return (Array.get(value, index));
        } else if (value instanceof List) {
            return ((List) value).get(index);
        } else {
            throw new IllegalArgumentException
                ("Non-indexed property for '" + name + "[" + index + "]'");
        }

    }


    /**
     * <p>Return the value of a mapped property with the specified name,
     * or <code>null</code> if there is no value for the specified key.
     * </p>
     *
     * @param name Name of the property whose value is to be retrieved
     * @param key Key of the value to be retrieved
     *
     * @exception IllegalArgumentException if there is no property
     *  of the specified name
     * @exception IllegalArgumentException if the specified property
     *  exists, but is not mapped
     */
    public Object get(String name, String key) {

        Object value = dynaValues.get(name);
        if (value == null) {
            throw new NullPointerException
                ("No mapped value for '" + name + "(" + key + ")'");
        } else if (value instanceof Map) {
            return (((Map) value).get(key));
        } else {
            throw new IllegalArgumentException
                ("Non-mapped property for '" + name + "(" + key + ")'");
        }

    }


    /**
     * <p>Return the value of a <code>String</code> property with the specified
     * name. This is equivalent to calling
     * <code>(String) dynaForm.get(name)</code>.</p>
     *
     * @param name Name of the property whose value is to be retrieved
     *
     * @throws IllegalArgumentException if there is no property
     *  of the specified name
     * @throws NullPointerException if the type specified for the
     *  property is invalid
     * @throws ClassCastException if the property is not a String.
     * @since Struts 1.2
     */
    public String getString(String name) {

        return (String) this.get(name);

    }


    /**
     * <p>Return the value of a <code>String[]</code> property with the
     * specified name. This is equivalent to calling
     * <code>(String[]) dynaForm.get(name)</code>.</p>
     *
     * @param name Name of the property whose value is to be retrieved

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -