background image

The Guess Number Example

<< Validation Model | Backing Beans >>
<< Validation Model | Backing Beans >>

The Guess Number Example

To handle navigation in the simplest application, you simply
Define the rules in the application configuration resource file.
Refer to an outcome String from the button or hyperlink component's action attribute.
This outcome String is used by the JavaServer Faces implementation to select the
navigation rule.
The Guess Number example uses this kind of simple navigation. Here is an example navigation
rule from the guessNumber application described in
"Defining Page Navigation" on page 296
:
<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 component on greeting.jsp is activated, the application
will navigate from the greeting.jsp page to the
tut-install/javaeetutorial5/examples/web/guessNumber/web/response.jsp page if the
outcome referenced by the button component's tag is success. Here is the commandButton tag
from greeting.jsp that specifies a logical outcome of success:
<h:commandButton id=
"submit" action="success"
value=
"Submit" />
As the example demonstrates, each navigation-rule element defines how to get from one page
(specified in the from-view-id element) to the other pages of the application. The
navigation-rule
elements can contain any number of navigation-case elements, each of
which defines the page to open next (defined by to-view-id) based on a logical outcome
(defined by from-outcome).
In more complicated applications, the logical outcome can also come from the return value of
an action method in a backing bean. This method performs some processing to determine the
outcome. For example, the method can check whether the password the user entered on the
page matches the one on file. If it does, the method might return success; otherwise, it might
return failure. An outcome of failure might result in the logon page being reloaded. An
outcome of success might cause the page displaying the user's credit card activity to open. If
you want the outcome to be returned by a method on a bean, you must refer to the method
using a method expression, using the action attribute, as shown by this example:
<h:commandButton id=
"submit"
action=
"#{userNumberBean.getOrderStatus}" value="Submit" />
When the user clicks the button represented by this tag, the corresponding component
generates an action event. This event is handled by the default ActionListener instance, which
Navigation Model
The Java EE 5 Tutorial · September 2007
308