Navigation Model

Virtually all web applications are made up of a set of pages. One of the primary concerns of a web application developer is to manage the navigation between these pages. The JavaServer Faces navigation model makes it easy to define page navigation and to handle any additional processing needed to choose the sequence in which pages are loaded.

As defined by JavaServer Faces technology, navigation is a set of rules for choosing the next page to be displayed after a button or hyperlink is clicked. These rules are defined by the application architect in the application configuration resource file (see Application Configuration Resource File) using a small set of XML elements.

To handle navigation in the simplest application, you simply

In more complicated applications, you also must provide one or more action methods, which perform some processing to determine what page should be displayed next. The component that triggers navigation references this method. The rest of this section describes what happens when that component is activated.

When a button or hyperlink is clicked, the component associated with it generates an action event. This event is handled by the default ActionListener instance, which calls the action method referenced by the component that triggered the event.

This action method is located in a backing bean and is provided by the application developer. It performs some processing and returns a logical outcome String, which describes the result of the processing. The listener passes the logical outcome and a reference to the action method that produced the outcome to the default NavigationHandler. The NavigationHandler selects the page to display next by matching the outcome or the action method reference against the navigation rules in the application configuration resource file.

Each navigation rule defines how to navigate from one particular page to any number of other pages in the application. Each navigation case within the navigation rule defines a target page and either a logical outcome, a reference to an action method, or both. Here is an example navigation rule from the guessNumber application described in Defining Page Navigation:

<navigation-rule>
  <from-view-id>/greeting.jsp</from-view-id>
  <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/response.jsp</to-view-id>
  </navigation-case>
</navigation-rule> 

This rule states that when the button or hyperlink component on greeting.jsp is activated, the application will navigate from the greeting.jsp page to the response.jsp page if the outcome referenced by the button or hyperlink component's tag is success.

The NavigationHandler selects the navigation rule that matches the page currently displayed. It then matches the outcome or the action method reference it received from the default ActionListener with those defined by the navigation cases. It first tries to match both the method reference and the outcome against the same navigation case. If that fails, it will attempt to match the outcome. Finally, it will attempt to match the action method reference if the previous two attempts failed.

When the NavigationHandler achieves a match, the render response phase begins. During this phase, the page selected by the NavigationHandler will be rendered.

For more information on how to define navigation rules, see Configuring Navigation Rules.

For more information on how to implement action methods to handle navigation, see Writing a Method to Handle an Action Event.

For more information on how to reference outcomes or action methods from component tags, see Referencing a Method That Performs Navigation.