Configuring Navigation Rules
As explained in Navigation Model (page 292), navigation is a set of rules for choosing the next page to be displayed after a button or hyperlink component is clicked. Navigation rules are defined in the application configuration resource file.
Each navigation rule specifies how to navigate from one page to a set of other pages. The JavaServer Faces implementation chooses the proper navigation rule according to which page is currently displayed.
After the proper navigation rule is selected, the choice of which page to access next from the current page depends on the action method that was invoked when the component was clicked and the logical outcome that is referenced by the component's tag or was returned from the action method.
The outcome can be anything the developer chooses, but Table 13-3 lists some outcomes commonly used in web applications.
Usually, the action method performs some processing on the form data of the current page. For example, the method might check whether the user name and password entered in the form match the user name and password on file. If they match, the method returns the outcome
success. Otherwise, it returns the outcomefailure. As this example demonstrates, both the method used to process the action and the outcome returned are necessary to determine the proper page to access.Here is a navigation rule that could be used with the example just described:
<navigation-rule> <from-view-id>/logon.jsp</from-view-id> <navigation-case> <from-action>#{LogonForm.logon}</from-action> <from-outcome>success</from-outcome> <to-view-id>/storefront.jsp</to-view-id> </navigation-case> <navigation-case> <from-action>#{LogonForm.logon}</from-action> <from-outcome>failure</from-outcome> <to-view-id>/logon.jsp</to-view-id> </navigation-case> </navigation-rule>This navigation rule defines the possible ways to navigate from
logon.jsp. Eachnavigation-caseelement defines one possible navigation path fromlogon.jsp. The firstnavigation-casesays that ifLogonForm.logonreturns an outcome ofsuccess, thenstorefront.jspwill be accessed. The secondnavigation-casesays thatlogon.jspwill be rerendered ifLogonForm.logonreturnsfailure.An application's navigation configuration consists of a set of navigation rules. Each rule is defined by the
navigation-ruleelement in thefaces-config.xmlfile.The navigation rules of the Duke's Bookstore application are very simple. Here are two complex navigation rules that could be used with the Duke's Bookstore application:
<navigation-rule> <from-view-id>/catalog.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/bookcashier.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>out of stock</from-outcome> <from-action>#{catalog.buy}</from-action> <to-view-id>/outofstock.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>error</from-outcome> <to-view-id>/error.jsp</to-view-id> </navigation-case> </navigation-rule>The first navigation rule in this example says that the application will navigate from
catalog.jsptoThe second navigation rule says that the application will navigate from any page to
error.jspif the application encountered an error.Each
navigation-ruleelement corresponds to one component tree identifier defined by the optionalfrom-view-idelement. This means that each rule defines all the possible ways to navigate from one particular page in the application. If there is nofrom-view-idelement, the navigation rules defined in thenavigation-ruleelement apply to all the pages in the application. Thefrom-view-idelement also allows wildcard matching patterns. For example, thisfrom-view-idelement says that the navigation rule applies to all the pages in thebooksdirectory:As shown in the example navigation rule, a
navigation-ruleelement can contain zero or morenavigation-caseelements. Thenavigation-caseelement defines a set of matching criteria. When these criteria are satisfied, the application will navigate to the page defined by theto-view-idelement contained in the samenavigation-caseelement.The navigation criteria are defined by optional
from-outcomeandfrom-actionelements. Thefrom-outcomeelement defines a logical outcome, such assuccess. Thefrom-actionelement uses a method expression to refer to an action method that returns aString, which is the logical outcome. The method performs some logic to determine the outcome and returns the outcome.The
navigation-caseelements are checked against the outcome and the method expression in this order:
- Cases specifying both a
from-outcomevalue and afrom-actionvalue. Both of these elements can be used if the action method returns different outcomes depending on the result of the processing it performs.- Cases specifying only a
from-outcomevalue. Thefrom-outcomeelement must match either the outcome defined by theactionattribute of theUICommandcomponent or the outcome returned by the method referred to by theUICommandcomponent.- Cases specifying only a
from-actionvalue. This value must match theactionexpression specified by the component tag.When any of these cases is matched, the component tree defined by the
to-view-idelement will be selected for rendering.Referencing a Method That Performs Navigation (page 370) explains how to use a component tag's
actionattribute to point to an action method. Writing a Method to Handle Navigation (page 409) explains how to write an action method.