We have already seen several aspects of RIFE that make common tasks easier. Another detail that makes RIFE very nice to use is the group of
setBean methods that makes it possible to fill out a lot of values in a template in one go. It simply matches the bean properties to template values, with an optional prefix.
Let's demonstrate this with the bean from Example 7.4, “Friend bean class”. The bean has properties for first and last name, a description and a URL. In Example 5.9, “The Java code used to add friends” we used
setValue to set the properties from the
Friend bean like this:
template.setValue("firstname", friend.getFirstname()); template.setValue("lastname", friend.getLastname()); template.setValue("description", friend.getDescription()); template.setValue("url", friend.getUrl());
Instead of calling
setValue for each property, using
setBean makes it possible to set all properties in one call:
This also gives makes it possible to add properties to a bean and template without having to edit the element backend code.
If there are several different beans used in the same template it's probably a good idea to prefix the various values to make it possible to easily see which bean a value belongs to. For example in the Friends database, we could prefix the value names with "FRIEND:". The interesting part of the template from Example 5.8, “Template for listing friends” would then become:
<!--B 'row'--> <tr valign="top"> <td><!--V 'FRIEND:firstname'/--></td> <td><!--V 'FRIEND:lastname'/--></td> <td> <a href="<!--V 'FRIEND:url'/-->"> <!--V 'FRIEND:description'/--> </a> </td> </tr> <!--/B-->
setBean method takes an optional
prefix argument. Calling
setBean with a prefix of "FRIEND:" will make the engine set the template value "FRIEND:firstname" to the value of the "firstname" bean property.