All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts.validator.LazyValidatorForm Maven / Gradle / Ivy

/*
 * $Id: LazyValidatorForm.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.validator;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.LazyDynaBean;

import java.util.List;
import java.util.Map;

/**
 * 

Struts Lazy ActionForm which wraps a * LazyDynaBean.

* *

There isn't really that much to this implementation as most of the * lazy behaviour is in LazyDynaBean and wrapping * the LazyDynaBean is handled in the parent * BeanValidatorForm. The only thing it really does is populate * indexed properties which are a List type with a * LazyDynaBean in the get(name, index) method.

* *

Lazy DynaBeans provide several types of lazy * behaviour:

* *
    * *
  • lazy property addition - properties which do not exist * are automatically added.
  • * *
  • lazy List facilities - automatically grows a * List or Array to accomodate the index value being * set.
  • * *
  • lazy List creation - automatic creation of a * List or Array for indexed properties, if * it doesn't exist.
  • lazy Map creation - automatic * creation of a Map for mapped properties, if it doesn't * exist.
  • * *
* *

Using this lazy ActionForm means that you don't have * to define the ActionForm's properties in the struts-config.xml. * However, a word of warning, everything in the Request gets populated into * this ActionForm circumventing the normal firewall * function of Struts forms. Therefore you should only take out of this * form properties you expect to be there rather than blindly populating all * the properties into the business tier.

* *

Having said that it is not necessary to pre-define properties in the * struts-config.xml, it is useful to sometimes do so for * mapped or indexed properties. For example, if you want to use * a different Map implementation from the default * HashMap or an array for indexed properties, rather than the * default List type:

* *

 *   <form-bean name="myForm" type="org.apache.struts.validator.LazyValidatorForm">
 *     <form-property name="myMap" type="java.util.TreeMap" />
 *     <form-property name="myBeans" type="org.apache.commons.beanutils.LazyDynaBean[]"
 * />
 *   </form-bean>
 * 
* *

Another reason for defining indexed properties in the * struts-config.xml is that if you are validating indexed * properties using the Validator and none are submitted then the indexed * property will be null which causes validator to fail. * Pre-defining them in the struts-config.xml will result in a * zero-length indexed property (array or List) being instantiated, avoiding * an issue with validator in that circumstance.

* *

This implementation validates using the ActionForm name. If you * require a version that validates according to the path then it can * be easily created in the following manner:

* *

 *    public class MyLazyForm extends LazyValidatorForm {
 *
 *        public MyLazyForm () {
 *            super();
 *            setPathValidation(true);
 *        }
 *
 *    }
 * 
* *

Rather than using this class, another alternative is to either use a * LazyDynaBean or custom version of LazyDynaBean * directly. Struts now automatically wraps objects which are not * ActionForms in a BeanValidatorForm. For * example:

* *

 *   <form-bean name="myForm" type="org.apache.commons.beanutils.LazyDynaBean">
 *     <form-property name="myBeans" type="org.apache.commons.beanutils.LazyDynaBean[]"
 * />
 *   </form-bean>
 * 
* * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005) * $ * @see Commons * BeanUtils JavaDoc * @since Struts 1.2.6 */ public class LazyValidatorForm extends BeanValidatorForm { // ------------------- Constructors ---------------------------------- /** * Default Constructor which creates a LazyDynaBean to * back this form. */ public LazyValidatorForm() { super(new LazyDynaBean()); } /** */ public LazyValidatorForm(DynaBean bean) { super(bean); } // ------------------- DynaBean methods ---------------------------------- /** *

Return an indexed property value.

* *

If the "indexed" property is a List type then any * missing values are populated with a bean (created in the * newIndexedBean(name) method - in this implementation this * is a LazyDynaBean type.

*/ public Object get(String name, int index) { int size = size(name); // Get the indexed property Object value = dynaBean.get(name, index); // Create missing beans for Lists if (value == null) { Object indexedValue = dynaBean.get(name); if (List.class.isAssignableFrom(indexedValue.getClass())) { for (int i = size; i <= index; i++) { value = newIndexedBean(name); set(name, i, value); } } } return value; } // ------------------- Public methods ---------------------------------- /** *

Return the Map containing the property values.

* *

Provided so that properties can be access using JSTL.

*/ public Map getMap() { return ((LazyDynaBean) dynaBean).getMap(); } // ------------------- Protected methods ---------------------------------- /** *

Creates new DynaBean instances to populate an 'indexed' * property of beans - defaults to LazyDynaBean type.

* *

Override this method if you require a different type of * DynaBean.

*/ protected DynaBean newIndexedBean(String name) { return new LazyDynaBean(); } }