Helper Beans

There is a second form of aggregation allowed with Tapestry components. The first way, covered previously, is to use embedded components to extend the functionality of the outer component. In some cases, useful behavior can be isolated, not into an additional component, but into a simple JavaBean.

These additional beans, called helper beans, are defined in the component specification, in the <bean> element. Each bean has a unique name, a class to instantiate, and a lifecycle (which controls how long the component keeps a reference to the bean). The specification allows properties of the bean to be set as well, using the <set-property> and <set-string-property> elements. Helper beans are accessed through the beans property of the component.

Beans are created as needed, they may then be cached for future use according to their declared lifecycle. The default lifecycle is request, meaning that the same bean will be returned until the end of the current request cycle.

An alternate lifecycle, page, means that once the bean is instantiated, it will continue to be available for the lifetime of the page containing it. Remember that helper beans should never contain any client-specific state, since a page will be used by multiple sessions and clients.

The last available lifecycle, none, indicates that the bean is not cached at all, and will be created fresh on each property access.

Tapestry includes a handful of useful helper beans. Default is used to provide default values for optional parameters. ValidationDelegate and several implementions of IValidator used with ValidField, it allows simple handling of validation and presentating validation errors. EvenOdd is used by the Tapestry Inspector; it generates a stream of values alternating between "even" and "odd"; this is combined with cascading stylesheets to make the rows alternate between white and grey backgrounds.