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

📄 dynaactionform.java

📁 structs源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: DynaActionForm.java 471754 2006-11-06 14:55:09Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 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;

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

import java.lang.reflect.Array;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * <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 $Rev: 471754 $ $Date: 2005-11-12 11:52:08 -0500 (Sat, 12 Nov 2005)
 *          $
 * @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);
    }

    /**
     * <p>Initialize the specified form bean.</p>
     *
     * @param config The configuration for the form bean to initialize.
     */
    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 the properties to their <code>initial</code> value if their
     * <code>reset</code> configuration is set to true or if
     * <code>reset</code> is set to a list of HTTP request methods that
     * includes the method of given <code>request</code> object.</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) {
        String name = getDynaClass().getName();

        if (name == null) {
            return;
        }

        FormBeanConfig config =
            mapping.getModuleConfig().findFormBeanConfig(name);

        if (config == null) {
            return;
        }

        // look for properties we should reset
        FormPropertyConfig[] props = config.findFormPropertyConfigs();

        for (int i = 0; i < props.length; i++) {
            String resetValue = props[i].getReset();

            // skip this property if there's no reset value
            if ((resetValue == null) || (resetValue.length() <= 0)) {
                continue;
            }

            boolean reset = Boolean.valueOf(resetValue).booleanValue();

            if (!reset) {
                // check for the request method
                // use a StringTokenizer with the default delimiters + a comma
                StringTokenizer st =
                    new StringTokenizer(resetValue, ", \t\n\r\f");

                while (st.hasMoreTokens()) {
                    String token = st.nextToken();

                    if (token.equalsIgnoreCase(request.getMethod())) {
                        reset = true;

                        break;
                    }
                }
            }

            if (reset) {
                set(props[i].getName(), props[i].initial());
            }
        }
    }

    // ------------------------------------------------------- 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
     * @return <code>true</code> if the specified mapped property contains a
     *         value for the specified key value; <code>true</code>
     *         otherwise.
     * @throws NullPointerException     if there is no property of the
     *                                  specified name
     * @throws IllegalArgumentException if there is no mapped 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
     * @return The value of a simple property with the specified name.
     * @throws IllegalArgumentException if there is no property of the
     *                                  specified name
     * @throws 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
     * @return The value of an indexed property with the specified name.
     * @throws IllegalArgumentException if there is no property of the
     *                                  specified name
     * @throws IllegalArgumentException if the specified property exists, but
     *                                  is not indexed
     * @throws 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
     * @return The value of a mapped property with the specified name, or
     *         <code>null</code> if there is no value for the specified key.
     * @throws NullPointerException     if there is no property of the
     *                                  specified name
     * @throws 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.
     * @return The value of a <code>String</code> property with the specified
     *         name.
     * @throws IllegalArgumentException if there is no property of the
     *                                  specified name

⌨️ 快捷键说明

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