Nested Beans

Back: BeanContexts Next: EnumeratedTypes

It is quite common for beans to reference other beans. WWB handles this for you. Let's look at net.sourceforge.wicketwebbeans.examples.nested.NestedBeanPage.

01 package net.sourceforge.wicketwebbeans.examples.nested;
02 
03 import net.sourceforge.wicketwebbeans.containers.BeanForm;
04 import net.sourceforge.wicketwebbeans.model.BeanMetaData;
05 
06 import org.apache.wicket.markup.html.WebPage;
07 
08 public class NestedBeanPage extends WebPage
09 {
10     public NestedBeanPage()
11     {
12         Customer bean = new Customer();
13         BeanMetaData meta = new BeanMetaData(bean.getClass(), null, this, null, false);
14         addnew BeanForm("beanForm", bean, meta) );
15     }
16 }
Java2html

Note that we only create an instance of net.sourceforge.wicketwebbeans.examples.nested.Customer. The bill-to and ship-to net.sourceforge.wicketwebbeans.examples.nested.Address objects are automatically instantiated by WWB.

Here are the beanprops.

	# Nested Bean Example
	Customer {
		props: 
			firstName, lastName, 
			billToAddress{colspan: 3}, 
			shipToAddress{colspan: 3};
	}
	
	Address {
		props: 
			address1{colspan: 3}, 
			address2{colspan: 3},
			city, state, zip; 
	}

Here we have specifications for the two beans we use: !Customer and !Address. The "colspan" specification tells BeanGridPanel to span the field across the three grid columns. In other words, this causes the field to span the entire row.

The result looks like:

Eclipse:/WicketWebBeans/doc/wiki/images/NestedBeanScreenshot.png

By default, nested objects are presented in a net.sourceforge.wicketwebbeans.fields.BeanGridField - which is a nested version of the BeanGridPanel used by BeanForm. There are other variations of nested bean fields, such as net.sourceforge.wicketwebbeans.fields.BeanInCollapsibleField, net.sourceforge.wicketwebbeans.fields.BeanInlineField, and net.sourceforge.wicketwebbeans.fields.BeanWithParentLabelField. If we wanted to use BeanInCollapsibleField rather than the default, we could say:

...	
	Customer {
		props: 
			firstName, lastName, 
			billToAddress{colspan: 3; fieldType: BeanInCollapsibleField }, 
			shipToAddress{colspan: 3; fieldType: BeanInCollapsibleField };
	}

Which results in collapsible fields for the Address beans:

Eclipse:/WicketWebBeans/doc/wiki/images/NestedBean2Screenshot.png

These fields have a bar across the top that allows you to expand and contract the content.

Back: BeanContexts Next: EnumeratedTypes