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.

<navigation-rule>
     <from-view-id>/pages/login.jsp</from-view-id>
     <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/pages/success.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
    <from-outcome>failure</from-outcome>
    <to-view-id>/pages/failure.jsp</to-view-id>
    </navigation-case>
</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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <display-name>BasicJSFJSPProject</display-name>
    <welcome-file-list>
            <welcome-file>index.html</welcome-file>
           <welcome-file>index.htm</welcome-file>
           <welcome-file>index.jsp</welcome-file>
           <welcome-file>default.html</welcome-file>
           <welcome-file>default.htm</welcome-file>
           <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
     <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
             <servlet-name>Faces Servlet</servlet-name>
             <url-pattern>/faces/*</url-pattern>
     </servlet-mapping>
     <context-param>
             <description>State saving method: 'client' or 'server' (=default). See JSF Specification    2.5.2</description>
             <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
             <param-value>client</param-value>
    </context-param>
    <context-param>
             <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
             <param-value>resources.application</param-value>
   </context-param>
  <listener>
             <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
   </listener>
   <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.jsf</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
           <servlet-name>Faces Servlet</servlet-name>
           <url-pattern>*.faces</url-pattern>
   </servlet-mapping>
</web-app>



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.