Java Reference
In-Depth Information
We can even execute the resolution returned by the event handler with
executeResolution=
"true".
Using an Action Bean-JSP Combination as a View Helper
Sometimes you'll find that after you've modularized the Java code in a
view helper class, you'd like to do the same for the block of JSP code
that uses it. The code that we used earlier to create the table of folders
was only a few lines long. But if you're doing a significant amount of
work, you may prefer to extract that block of code into a separate JSP
and keep the original JSP from getting too lengthy.
To accomplish this, you can write a view helper as an action bean-JSP
combination. The action bean is a view helper class and has a default
event handler that forwards to a JSP. The JSP contains the block of view
code that uses the information provided by the action bean to produce
the desired portion of the view. You can then “embed” the result in the
original JSP.
Let's see how that technique works by using it to implement the menu
that appears at the top of each page:
We'll start with the
MenuViewHelper
action bean. It defines the sections
of the application (Messages, Contact List, and Compose) and makes
them available in a getter method. It also has a property to hold the
currently selected section:
package
stripesbook.action;
public class
MenuViewHelper
extends
BaseActionBean {
public
Section[] getSections() {
return
Section.values();
}
private
Section currentSection;
public
Section getCurrentSection() {
return
currentSection;
}
public void
setCurrentSection(Section currentSection) {
this
.currentSection = currentSection;
}
@DefaultHandler
public
Resolution view() {
return new
ForwardResolution("/WEB-INF/jsp/common/menu.jsp");
}
Search WWH ::
Custom Search