| > Home | V Lazy ActionForm |
Lazy DynaBean |
![]() |
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.
...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!!!
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.
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() {
super();
}
/**
* Implements a |
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.
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.