ArticlesTutorialsInterview Questions & AnswersCertificationsFree Books & Sample ChaptersTechnical Discussions  Your Account | Advertise with us | Feedback |
Few pages of this site are under construction and we are constantly trying to improve it. If you have any suggestions or ideas about how to improve the site, please Let us know.
JavaServer Faces (JSF) Tutorial

Contents
JSF Introduction
JSF Lifecycle
Getting started
JSF Example
Resources
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="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
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     
version="2.5">
     
<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>

      <welcome-file-list>
           
<welcome-file>index.jsp</welcome-file>
     
</welcome-file-list>
</
web-app>

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.

........
url-pattern>/faces/*</url-pattern>

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="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
       http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
     
version="1.2">

      <navigation-rule>
           
<from-view-id>/index.jsp</from-view-id>
           
<navigation-case>
                 
<from-outcome>sayHello</from-outcome>
                 
<to-view-id>/hello.jsp</to-view-id>
           
</navigation-case>
           
<navigation-case>
                 
<from-outcome>sayBye</from-outcome>
                 
<to-view-id>/bye.jsp</to-view-id>
           
</navigation-case>
     
</navigation-rule>
 

      <managed-bean>
           
<managed-bean-name>person</managed-bean-name>
           
<managed-bean-class>
                  com.developersBook.jsf.bean.PersonBean
           
</managed-bean-class>
           
<managed-bean-scope>request</managed-bean-scope>
     
</managed-bean>
 

      <navigation-rule>
           
<from-view-id>*</from-view-id>
           
<navigation-case>
                 
<from-outcome>index</from-outcome>
                 
<to-view-id>/index.jsp</to-view-id>
           
</navigation-case>
     
</navigation-rule>
</
faces-config>



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:#{person.name}) 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.


Conclusion

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.


Resources

  • 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 »
Related Links

  ALSO SEE :

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