faces-config.xml to DirectTraffic in the JSF Application
By: Abinaya in JSF Tutorials on 2007-10-06
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
<convertNumber> and <convertDateTime> in JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
How to open a new browser window from my JSF page?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Comments