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 link 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 Navigation Model.
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 action method invoked when the component was clicked
-
The logical outcome referenced by the component’s tag or returned from the action method
The outcome can be anything the developer chooses, but Table 16-3 lists some outcomes commonly used in web applications.
Table 16-3 Common Outcome Strings
Outcome |
What It Means |
|
Everything worked. Go on to the next page. |
|
Something is wrong. Go on to an error page. |
|
The user needs to log in first. Go on to the login page. |
|
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 the following 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 thejavax.faces.component.UICommandcomponent 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-id element will be selected for rendering.