Sunday, April 19, 2009

JSF Page Navigation - 2

<< Previous        Next >>

JSF Page Navigation - Part 2

Each navigation rule defines how to navigate from one particular page to a set of other pages in the application. A <navigation-rule> element can have zero or more <navigation-case> sub-elements. The <navigation-case> element defines a set of matching criteria defined by logical outcome of the action method referenced by the button or hyperlink . 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.

In our example,the navigation rule says that when the button component on UserDetailsForm.jsp is activated, the application will navigate from the UserDetailsForm.jsp page to the userDetailsSubmitted.jsp page if the outcome referenced by the button component's tag is "submitted".
The theory behind is when a button or hyperlink is clicked, the component associated with it generates an action event. Normally, to capture an ActionEvent, you need to write an ActionListener. In JSF this event is handled by the default ActionListener for page navigation. The ActionListener instance 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 must be a public method that takes no parameters and returns a  logical outcome String, The outcome can be anything the developer chooses. In our example the action method retuns an outcome "submitted". 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.
In some case there can be more than one navigation case for a navigation rule.For example,in a login page if the username and password entered by the user matches those in the database,the action method might return "success"; otherwise, it might return "failure".An outcome of failure might result in failure.jsp page. An outcome of success might redirect to success.jsp page.In this case we will see how we will define navigation rule.


The web.xml file of the applicaiton :

The complete listing of web.xml is shown below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
            <servlet-name>Faces Servlet</servlet-name>
             <servlet-name>Faces Servlet</servlet-name>
             <description>State saving method: 'client' or 'server' (=default). See JSF Specification    2.5.2</description>
            <servlet-name>Faces Servlet</servlet-name>
           <servlet-name>Faces Servlet</servlet-name>

Run the application.

To run your web application, right click on BasicJSFProject, click- >run as -> run on server.
Select the server type ,Apache Tomacat v6.0 Server.Click Fininsh.

Open a web browser and type the url : http://localhost:8080/BasicJSFProject/

Enter valid inputs in the first page and click submit.It will be redirected to next page with the contents as shown below.

<< Previous       Next  >>

See More Topics:

How to use resource bundle in JSF?
How to implement Internationalization and Localization in JSF?
How event handling works in JSF?
JSF Request Processing Life Cycle with example.