background image

The NavigationHandler

<< Writing Backing Bean Methods | Action Event Handler >>
<< Writing Backing Bean Methods | Action Event Handler >>

The NavigationHandler

After setting the components' rendered properties to true, this method returns the logical
outcome null. This causes the JavaServer Faces implementation to re-render the
bookcashier.jsp
page without creating a new view of the page. If this method were to return
purchase
(which is the logical outcome to use to advance to bookcashier.jsp, as defined by
the application configuration resource file), the bookcashier.jsp page would re-render
without retaining the customer's input. In this case, you want to re-render the page without
clearing the data.
If the user does not purchase more than $100 (or 100 euros) worth of books or the thankYou
component has already been rendered, the method returns receipt.
The default NavigationHandler provided by the JavaServer Faces implementation matches the
logical outcome, as well as the starting page (bookcashier.jsp) against the navigation rules in
the application configuration resource file to determine which page to access next. In this case,
the JavaServer Faces implementation loads the bookreceipt.jsp page after this method
returns.
public String submit() {
...
if(cart().getTotal() > 100.00 &&
!specialOffer.isRendered())
{
specialOfferText.setRendered(true);
specialOffer.setRendered(true);
return null;
} else if (specialOffer.isRendered() &&
!thankYou.isRendered()){
thankYou.setRendered(true);
return null;
} else {
clear();
return (
"receipt");
}
}
Typically, an action method will return a String outcome, as shown in the previous example.
Alternatively, you can define an Enum class that encapsulates all possible outcome strings and
then make an action method return an enum constant, which represents a particular String
outcome defined by the Enum class. In this case, the value returned by a call to the Enum class's
toString
method must match that specified by the from-outcome element in the appropriate
navigation rule configuration defined in the application configuration file.
The Duke's Bank example uses an Enum class to encapsulate all logical outcomes:
public enum Navigation
{
main, accountHist, accountList, atm, atmAck, transferFunds,
transferAck, error
}
Writing Backing Bean Methods
Chapter 12 · Developing with JavaServer Faces Technology
405