faces-config.xml to DirectTraffic in the JSF Application

By: Abinaya Emailed: 1588 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Information about the view components in the web application and information about how control flows through the application is contained in a special configuration file named faces-config.xml, shown in Listing below. Although faces-config.xml can contain a lot of information about a web application, for this example we need it to do only two things: identify the flow of control from searchForm.jsp to searchResults.jsp, and identify the JavaBean used by the application.

faces-config.xml

<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= ➥
"http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<navigation-rule>
<from-view-id>/searchForm.jsp</from-view-id>
<navigation-case>
<from-outcome>submit</from-outcome>
<to-view-id>/searchResults.jsp</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>flight</managed-bean-name>
<managed-bean-class>com.apress.projsp.FlightSearch</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>

The faces-config.xml file identifies the JavaBeans used by the web application in the <managed-bean> element. You will have a <managed-bean> element for every JavaBean used by your web application. The <managed-bean> element in Listing above contains three subelements:

  • The first subelement is the name used to identify the JavaBean in a JSP page. In Listing above
    the name is given as flight; this is why both searchForm.jsp and searchResults.jsp can access an instance of the JavaBean by using the expression #{flight...}.
  • The second element is the fully qualified class name of the JavaBean class. This name tells the JSP container which class to load and instantiate to create an instance of the JavaBean.
  • The third element identifies the scope of the object. Session scope means that the object exists for the entire interaction between the user and the application. The container must persist the object across multiple request/response cycles, until the user’s session is terminated. 

The faces-config.xml file is also used to tell the controller how to navigate through the application. Navigation flow is specified in <navigation-rule> elements. Our example needs only one element. In general, a <navigation-rule> element identifies the start page, a condition, and the page to navigate to when the condition occurs.

In our example, the start page is searchForm.jsp. If the page request is submitted with an outcome of submit, control is transferred to searchResults.jsp. Looking at Listing, you can see that the <commandButton> element has an action of submit; when the button is clicked and the form is submitted, this action matches the <from-outcome> of the <navigation-rule>. The <navigation-rule> element also includes an empty <redirect> element. With this element, the response is created by causing the browser to redirect to the searchResults.jsp page, which also updates the address bar in the browser. Without this element, the response is still created correctly and sent to the browser, but the address bar of the browser will not be updated and will still display the address for the originating page. 

We need just one final piece for our web application. In this example, our default page will be a standard HTML page that redirects to the correct URL for a JSF application. Listing below shows index.html.

index.html

<html>
<head>
<meta http-equiv="Refresh" content= "0; URL=searchForm.faces"/>
</head>
</html>

You can see that that the redirect URL is searchForm.faces. However, there is no component in our application named searchForm.faces. How then does the web application know which page to serve? All requests that are JSF requests are directed to the controller for the application, which is a servlet supplied as part of the JSF reference implementation.While deploying this example, you will specify that all URLs of the form *.faces should be sent to the controller servlet. This servlet then converts the searchForm.faces request to searchForm.jsp, processes the JSP page, and sends the response to the browser.


JSF Home | All JSF Tutorials | Latest JSF Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Abinaya
How to compile a Java program - javac
The clone() Method in Java
Data Types in Java
JSP Example to connect to MS SQL database and retrieve records
faces-config.xml to DirectTraffic in the JSF Application
Enabling Expression Language Evaluation in JSP
Using malloc() Function in C
ActionErrors and ActionError in Struts
Open, Creat, Close, Unlink system calls sample program in C
Structures and Functions in C
Introduction to JSP expression language
Multi-dimensional Arrays in C (Explained using date conversion program)
A C program similar to grep command in UNIX
Type Conversions in C (String to Integer, isdigit() etc)
Basics of C

More Tutorials in JSF
Struts Vs JSF (A comparison of Struts against JSF)
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
Action listeners in JSF
Calling Multiple Listeners in JSF
Using Javascript in JSF
How to open a new browser window from my JSF page?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
JSF - TreeNode.setID gets IllegalArgument Exception
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle

More Latest News
Most Viewed Articles (in JSF )
Install and Deploy JBoss Application Server
How to open a new browser window from my JSF page?
Java Bean Scopes in JSF
Action listeners in JSF
Using Javascript in JSF
Calling Multiple Listeners in JSF
JSF Life Cycle
What is JSF (JavaServer Faces)?
<convertNumber> and <convertDateTime> in JSF
Struts Vs JSF (A comparison of Struts against JSF)
Accessing Context Data in Beans using JSF
JSF Basics
faces-config.xml to DirectTraffic in the JSF Application
Differences between JavaServer Faces technology and Struts
Installing JSF
Most Emailed Articles (in JSF)
JSF Basics
JSF Life Cycle
How to open a new browser window from my JSF page?
<convertNumber> and <convertDateTime> in JSF
Calling Multiple Listeners in JSF
Installing JSF
faces-config.xml to DirectTraffic in the JSF Application
Action listeners in JSF
Struts Vs JSF (A comparison of Struts against JSF)
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using Javascript in JSF
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
Java Bean Scopes in JSF