Introduction To Java Server Faces (JSF)

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 easy data transfer between UI components.

Architecture of JSF

A JSF application is similar to any Java-based web application. It runs in a Java Servlet container and it 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 the processing related to JSF is done inside the container. The procedure said below explains how a JSF application works in a scenario where a user clicks on some button or performs any other event and gets 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.    <servlet-name>Faces Servlet</servlet-name>  
    3.    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>  
    4.       <load-on-startup>1</load-on-startup>  
    5.    </servlet>  
    6.    <Servlet-mapping>  
    7.    <servlet-name>Faces Servlet</servlet-name>  
    8.    <url-pattern>/faces/*</url-pattern>  
    9. </servlet-mapping> 
  2. All 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 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.    <managed-bean>  
      3.       <managed-bean-name>someName</managed-bean-name>  
      4.       <managed-bean-class>  
      5.          somePackage.SomeClass  
      6.       </managed-bean-class>  
      7.    </managed-bean>  
      8. </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.   
      3.   
      4. @ManagedBean(name="JSFTut",eager=true)  
      5. public class JSFTutorials {  
      6.         public String callMe()  
      7.     {  
      8.         return "Hello JSF ";  
      9.     }  

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.

Up Next
    Ebook Download
    View all
    Learn
    View all