Java Reference
In-Depth Information
View helpers are awesome for dynamic content in reusable layouts.
Let's break for a short walk and some fresh air before looking at view
helpers.
7.3
Using View Helpers
When you're creating a view by writing a JSP, you have access to the
current action bean with ${actionBean} and can use the information
it provides to build the view. However, sometimes you'll find that the
code to provide the data you need doesn't belong in the current action
bean. This is often the case when working with layouts, because you're
creating a view that will be reused across several pages. The current
action bean will be different on each page. In these situations, you'll
want to use a view helper.
Using a Simple Bean as a View Helper
A view helper is a block of Java code that is independent of the current
action bean and makes your life easier when you're building dynamic
parts of a view. You write the Java code that does the work and then
use the results in the JSP to create the view. Let's look at a simple
example of how it's done.
At the bottom of Figure 7.2 , on page 153 , we see that both the Message
List and Message Details pages have a table on the left. The table shows
the folders with the number of messages in each folder. We need the list
of folders in layout_folders.jsp to create the table.
Say we have a Folder class to represent a folder:
Download email_16/src/stripesbook/model/Folder.java
package stripesbook.model;
public class Folder extends ModelBase {
private String name;
private int numberOfMessages;
/ * getters and setters... * /
}
Suppose we have a FolderDao implementation that provides the list of
folders with its read ( ) method. We need a block of code that retrieves
the list and makes it available to the JSP. That doesn't belong in any
specific action bean, so we write a view helper.
 
 
 
 
Search WWH ::




Custom Search