> Home V Lazy ActionForm

Lazy DynaBean

Holiday Accommodation in Croatia

1. Information

Lazy DynaBeans have been donated to Jakarta Commons BeanUtils and were released in BeanUtils version 1.7.0.

For information on LazyDynaBean see the BeanUtils Package JavaDoc and LazyDynaBean JavaDoc.

See also the Lazy ActionForm page.

2. Did You Know...?

...that since Struts 1.2.4 you can plug a LazyDynaBean directly into Struts without it having to extend ActionForm. It now will automatically wrap it in a BeanValidatorForm.

      <form-bean name="LazyBean" type="org.apache.commons.beanutils.LazyDynaBean"/>

...in fact you can plug any DynaBean that uses a DynaClass that implements the MutableDynaClass interface directly into Struts in this way and configure its properties through the struts-config.xml.

...in fact you can also plug any POJO Bean directly into Struts now. It first wraps the POJO Bean in a WrapDynaBean and then wraps that WrapDynaBean in a BeanValidatorForm!!!

3. Nested LazyDynaBean

The default indexed property for LazyDynaBean is a java.util.List. However if you want a bean that will automatically nest to n levels then changing the default behaviour to return an Array of itself is very straight forward (override defaultIndexedProperty() method) and produces a bean with great behaviour.

3.1 NestedLazyBean Code

Below is the code for such a Nested LazyDynaBean and then after that an example of using it (if you want a copy of the source click here).

import org.apache.commons.beanutils.LazyDynaBean;

public class NestedLazyBean extends LazyDynaBean {

    * Default Constructor. 
    public NestedLazyBean() {
     * Implements a NestedLazyBean[] as the default indexed
     * property implementation.
    protected Object defaultIndexedProperty(String name) {
        return new NestedLazyBean[0];
     * Returns Null removing some of the automatic object
     * instantiation that I found too aggressive.
    protected Object createOtherProperty(String name, Class type) {
        return null;


3.2 Using the NestedLazyBean

Say, for example, you wanted an Order Book bean which contained all your customers. Each customer contained all their orders and each order contained all the products on that order. Using such a Nested bean you could populate it as follows:

        // Create the 'Order Book' bean
        DynaBean orderBook = new NestedLazyBean();

        // Add the customers
        for (int c = 0; c < custCount; c++) {

            // Indexed get sets up list property and generates
            // a new 'customer' bean automatically
            DynaBean customer = (DynaBean)orderBook.get("customer", c);
            customer.set("name",    "...");
            customer.set("address", "...");
            customer.set("phone",   "...");

            // Add the orders
            for (int o = 0; o < ordCount; c++) {

                DynaBean order = (DynaBean)customer.get("order", o);
                order.set("orderDate",    "...");
                order.set("deliveryDate", "...");

                // Add the ordered products
                for (int p = 0; p < prodCount; c++) {

                    DynaBean product = (DynaBean)order.get("product", p);
                    product.set("code",    "...");
                    product.set("description", "...");
                    product.set("price", "...");


Plug this type of Bean into Struts along with the Nested taglib and you have a very powerful combination, doing the population shown in the example above, all automatically.

You can either plug this bean directly into Struts or use it in your own custom Lazy ActionForm - the NestedLazyForm is such an implementation using this DynaBean.

4. Automatic Property Instantiation

A few people have commented that they're not happy with the automatic object instantiation in LazyDynaBean - even when you just do a get() on a property. To be honest, I agree, it is too aggressive. The good thing about the way they're designed is that its easy to create a custom version and modify this behaviour. Take a look at the createProperty() method - it calls six other methods to instantiate various object types - any or all of which can be overriden. For me I'm happy for it to automatically instantiate Lists, Arrays, Maps, DynaBeans and primitives - but I don't like the other object types it tries to do. To remove this behaviour all you have to do is override the createOtherProperty() method and return null. The NestedLazyBean example above does this.