Configuring Navigation Rules
Navigation between different pages of a JavaServer Faces application, such as choosing the next page to be displayed after a button or hyperlink component is clicked, is defined by a set of rules. Navigation rules can be implicit, or they can be explicitly defined in the application configuration resource file. For more information on implicit navigation rules, see Implicit Navigation Rules.
Each navigation rule specifies how to navigate from one page to another page or set of 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 two factors:
The outcome can be anything the developer chooses, but Table 14-3 lists some outcomes commonly used in web applications.
Outcome |
What It Means |
---|---|
success |
Everything worked. Go on to the next page. |
failure |
Something is wrong. Go on to an error page. |
login |
The user needs to log in first. Go on to the login page. |
no results |
The search did not find anything. Go to the search page again. |
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 outcome failure. As this example demonstrates, both the method used to process the action and the outcome returned are necessary to determine the correct page to access.
Here is a navigation rule that could be used with the example just described:
<navigation-rule> <from-view-id>/login.xhtml</from-view-id> <navigation-case> <from-action>#{LoginForm.login}</from-action> <from-outcome>success</from-outcome> <to-view-id>/storefront.xhtml</to-view-id> </navigation-case> <navigation-case> <from-action>#{LoginForm.logon}</from-action> <from-outcome>failure</from-outcome> <to-view-id>/logon.xhtml</to-view-id> </navigation-case> </navigation-rule>
This navigation rule defines the possible ways to navigate from login.xhtml. Each navigation-case element defines one possible navigation path from login.xhtml. The first navigation-case says that if LoginForm.login returns an outcome of success, then storefront.xhtml will be accessed. The second navigation-case says that login.xhtml will be re-rendered if LoginForm.login returns failure.
The configuration of an application’s page flow consists of a set of navigation rules. Each rule is defined by the navigation-rule element in the faces-config.xml file.
Each navigation-rule element corresponds to one component tree identifier defined by the optional from-view-id element. This means that each rule defines all the possible ways to navigate from one particular page in the application. If there is no from-view-id element, the navigation rules defined in the navigation-rule element apply to all the pages in the application. The from-view-id element also allows wildcard matching patterns. For example, this from-view-id element says that the navigation rule applies to all the pages in the books directory:
<from-view-id>/books/*</from-view-id>
A navigation-rule element can contain zero or more navigation-case elements. The navigation-case element defines a set of matching criteria. When these criteria are satisfied, the application will navigate to the page defined by the to-view-id element contained in the same navigation-case element.
The navigation criteria are defined by optional from-outcome and from-action elements. The from-outcome element defines a logical outcome, such as success. The from-action element uses a method expression to refer to an action method that returns a String, which is the logical outcome. The method performs some logic to determine the outcome and returns the outcome.
The navigation-case elements are checked against the outcome and the method expression in this order:
Cases specifying both a from-outcome value and a from-action value. 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-outcome value. The from-outcome element must match either the outcome defined by the action attribute of the javax.faces.component.UICommand component or the outcome returned by the method referred to by the UICommand component.
Cases specifying only a from-action value. This value must match the action expression specified by the component tag.
When any of these cases is matched, the component tree defined by the to-view-id element will be selected for rendering.
To Configure a Navigation Rule
Using NetBeans IDE, you can configure a navigation rule by doing the following.
- After opening your project in NetBeans IDE, expand the project node in the Projects pane.
- Expand the Web Pages and WEB-INF nodes of the project node.
- Double-click faces-config.xml.
- After faces-config.xml opens in the editor pane, right-click in the editor pane.
- From the Insert menu, choose Navigation Rule.
- In the Add Navigation Rule dialog:
- Enter or browse for the page that represents the starting view for this navigation rule.
- Click Add.
- Right-click again in the editor pane.
- From the Insert menu, choose Navigation Case.
- In the Add Navigation Case dialog box:
- From the From View menu, choose the page that represents the starting view for the navigation rule (from Step 6 a).
- (optional) In the From Action field, type the action method invoked when the component that triggered navigation is activated.
- (optional) In the From Outcome field, enter the logical outcome string that the activated component references from its action attribute.
- From the To View menu, choose or browse for the page that will be opened if this navigation case is selected by the navigation system.
- Click Add.
See Also
Referencing a Method That Performs Navigation explains how to use a component tag’s action attribute to point to an action method. Writing a Method to Handle Navigation explains how to write an action method.
Implicit Navigation Rules
JavaServer Faces technology supports implicit navigation rules for Facelets applications. Implicit navigation applies when navigation-rules are not configured in the application configuration resource files.
When you add a component such as a commandButton in a page, and assign another page as the value for its action property, the default navigation handler will try to match a suitable page within the application implicitly.
<h:commandButton value="submit" action="response">
In the above example, the default navigation handler will try to locate a page named response.xhtml within the application and navigate to it.