Introduction To Java Server Faces (JSF)

Introduction

 
Java Server Faces (JSF), is a web-based framework that makes the UI development in web-based applications easy.  JSF provides various ways to connect UI components with data sources and server-side event handlers.  It helps in making data transfer between UI components easy.
 

Architecture of JSF

 
A JSF application is similar to any Java-based web application.  It runs in a Java Servlet container and follows the MVC (Model, View and Controller) architecture.
  
The diagram below shows the architecture of a JSF application:
 
JFS
 

How a JSF works

 
JSF applications run inside a servlet container.  All of the processing related to JSF, is done inside the container.  The procedure below, explains how a JSF application works, in a scenario where a user clicks button or performs any other event, to get the desired output.   
  1. When a user fires an event, it goes to a FacesServlet class, that is an engine of a JSF application.  A servlet-mapping element is used in a deployment descriptor (web.xml), to map a specific pattern, to invoke the FacesServlet class.  The following code is a part of the web.xml used to run a JSF application. 
    1. < Servlet >  
    2.  <  
    3.  servlet - name > Faces Servlet < /servlet-name>   <  
    4.  servlet - class > javax.faces.webapp.FacesServlet < /servlet-class>   <  
    5.  load - on - startup > 1 < /load-on-startup>   <  
    6.  /servlet>   <  
    7.  Servlet - mapping >  
    8.  <  
    9.  servlet - name > Faces Servlet < /servlet-name>   <  
    10.  url - pattern > /faces/ * < /url-pattern>   <  
    11.  /servlet-mapping>   
  2. All of the processing is done inside the FacesServlet class.  Each JSF application, inside the same web container, has its own FacesServlet class.
     
  3. Each FacesServlet class has a FacesContext class object, that has all of the necessary information, of the current request.
     
  4. Now, the request goes to the called ManagedBean and it returns the output to the called UI page.
     
  5. There are two ways by which you can map your JSF/JSP page with the required managedbean class.
     
    1. Using Faces-Config.xml file: Faces-Config XML file has all the information of mapping between JSP/JSF pages and managedbean class. An example of a Faces-Config XML file is shown below: 
      1. < faces - config >  
      2.  <  
      3.  managed - bean >  
      4.  <  
      5.  managed - bean - name > someName < /managed-bean-name>   <  
      6.  managed - bean - class >  
      7.  somePackage.SomeClass <  
      8.  /managed-bean-class>   <  
      9.  /managed-bean>   <  
      10.  /faces-config>  
    2. Using Annotations: Another way to map your JSP/JSF file, with the managedbean class, is to use Annotations in the managedbean.  An example of annotation is shown below:
      1. Import javax.faces.bean.ManagedBean;  
      2. @ManagedBean(name = "JSFTut", eager = true)  
      3. public class JSFTutorials {  
      4.  public String callMe() {  
      5.   return "Hello JSF ";  
      6.  }  
      7. }  
      In future articles, I will be describing more about the JSF framework.  I hope this article was useful to you and thank you for reading it.


Similar Articles