ArticlesTutorialsInterview Questions & AnswersCertificationsFree Books & Sample ChaptersTechnical Discussions  Your Account | Advertise with us | Feedback |
JavaServer Faces (JSF) Tutorial

JSF Introduction
JSF Lifecycle
Getting started
JSF Example
JSF Standard Components
JSF Tomahawk Components

JavaServer Faces (JSF) Tutorial

                     By Anand K Reddy , developersBOOK.COM , 03/01/08

Page 3 of 3

When working with JSF, you will have a minimum of two XML configuration files, and you will often have even more (ex: tiles.xml). It is important that you become familiar with these config files, as they are the key to the flexibility and loose coupling provided by this architecture.

  • Faces config (faces-config.xml) — JavaServer Faces configuration file. Place this file in the WEB-INF directory. This file lists bean resources and navigation rules.
  • Web config (web.xml)—this is your standard Web configuration file.

The next step is to create our /WEB-INF/web.xml descriptor file for our hello web application.

Code Listing 4: web.xml

<?xml version="1.0"?>
web-app xmlns=""
<servlet-name>Faces Servlet</servlet-name>

<servlet-name>Faces Servlet</servlet-name>


This is your regular web-config file, except the servlet mapping. All JSF pages are processes by a special servlet (FacesServlet) that is a part of the JSF implementation code. The JSF URLs have a special format to ensure that the correct servlet is activated when a JSF page is requested. In our example, they have an extension .faces.
Ex: http://localhost:8080/hello/index.faces.

Note: You can also define a prefix mapping instead of the .faces extension mapping.


You are probably wondering how all of this is going to tie together. How does PersonBean get into JSP's and how do the input buttons on index.jsp get me to hello.jsp or bye.jsp? That's where we need a faces-config (faces-config.xml) file.

To cause JSF to proceed from one page to another, you must inform it of the page flows of your application. We do this with navigation rules, which predicate the choice of response view on the outcome of a request. Define navigation rules in your faces-config file.
Create a file called /WEB-INF/faces-config.xml

Code Listing 5: faces-config.xml

<?xml version="1.0"?>

<faces-config xmlns=""




Navigation Rules

  • Navigation rules define almost all the page-flow logic of a JSF application.
  • A navigation rule tells the JSF runtime:
    • For what origin we are setting a rule – this is <from–view–id>
    • Where to direct the control in what cases.
  • So JSF takes the decision of what page follows what other page away from the page definition, and puts it in the configuration. It decides where to go next based on the identity of the requesting view, plus the outcome.

Figure 4: JSF Navigation Rules
JSF Navigation Rules

In particular, if we're coming from /index.jsp and the "outcome" is "sayHello", go to the hello.jsp view; if the "outcome" is "sayBye", go to the bye.jsp view. The "outcome" is determined by the action property of the commandButton elements your "from-view", index.jsp.

By contrast, simple HTML forms, and Struts, and Spring, all let the view define the request URLs that originate from various HTML forms, buttons, and links.

Additionally, you can see we've created a managed-bean called person that is an instance of our com.developersBook.jsf.PersonBean class. personBean has request scope, and is therefore available throughout processing of the entire request. Its properties can be accessed and set using the #{beanname.propertyname}( Ex:#{}) convention, as shown in all three JSPs.

Figure 5: Binding UI to Managed Bean
Binding UI to Managed Bean

With all this in place, you should now be able to compile, build, deploy and run your web application. You can download the code of this tutorial here.


JavaServer Faces offers an extremely powerful and flexible framework. Use its power to fit your application’s needs. And don’t forget that it’s all about the lifecycle.


  • You can download the source code of this tutorial  here.
  • JavaServer Faces homepage.
  • Discuss about this tutorial here.
Related Books

Next » JSF Standard Components with examples....

« Previous | 1 2 3 |Next »


About Us | Our Mission | Terms of use | Author Terms |    Search        | Contact Us | Advertise With Us | Link to Us | Sitemap
Copyright © 2016 developersBOOK.COM. All rights reserved.