Java Server Faces Tutorial

Introduction to JSF

Java Server Faces (JSF) is server side technology developed by Sun Microsystems. JSF is component based and event driven framework for developing Java web applications and simplifies the development of Java based web applications.

JSF integrate the Model-View-Controller (MVC) for the development of component which ensures that the applications are well designed with greater maintainability. All JSF components perform following three fundamental tasks.

  • * Render the component, typically by generating markup.
  • * Handle the component's events.
  • * Validate the component's values
  • 1. JSF 1.2 (11 may 2006) - Latest release of JSF specification.
  • 2. JSF 1.1(27 may 2004) - Bug fix release with No specification changes and HTML render kit changes but performance improvement.
  • 3. JSF 1.0 (11 mar 2004) - Initial release of JSF specification.

JSF 1.0 supports Servlet 2.3 and JSP 1.2. After JSF 1.0, JSF 1.1 was released. The main purpose of this was to solve the problem of bug-fixing. There were no new specification or HTML render kit changes. This version was also compatible with same version of Servlet 2.3 and JSP 1.2 as in the case of JSF 1.0.

JSF 1.1_01 release has developed features for bug fixes and performance improvements. Improvements include compression of views serialized to the client, failover support while storing views on the server rendered fixes. It supports JDK 1.3.1 or later.

JSF 1.2 is the latest release and it works with servlet 2.5 and JSP 2.1. If we want to run JSF 1.2 on Tomcat then we need 6.0 not 5.5. So JSF needs a web container that supports at least Servlet 2.3 and JSP 1.2 and these are part of J2EE1.3.

Features of JSF 1.2

  • Unified Expression Language (EL):- The unified EL has been added to JSTL for solving the problems when integrating with JSP EL with the help of JSF EL.
  • Support for AJAX i.e. Asynchronous Java Script and XML file
  • Issues related with navigation button on Multi Frame or Multi Window Faces Apps.
  • Giving XML schema Support for XML config files.
  • Better Security Support Features and java script validation.

The given below is the request handling mechanism by the JSF components. The process of control is shown with solid lines and the dashed lines shows the alternate flow for a page redisplay and validation request or in the event of a conversion error.

Benefits of JSF

The following are the advantages or benifts of using JSF:-

  • It gives standard, reusable components for creating user interfaces for web applications.
  • It has many tag libraries for accessing and manipulating the components.
  • It automatically saves the form data and repopulates the form when it is displayed at client side.
  • It encapsulates the event handling and UI logic into a single component which can be reused by various web applications.

Life Cycle of JSF Application

The life cycle is defined as the processes and phases needs to go through to generate the response for a given request. For JSF, it consists of following six steps that an application follows when any request comes to the system for the processing.

Life Cycle of phase can be described as:

Phase 1: Restore View

A request comes via the FacesServlet controller. It examines the request and extracts the view ID that is determined by the name of the JSP page. The JSF framework controller uses the view ID to look up the components for the current view. If the view doesn't already exist, the JSF controller creates it. If the view already exists, the JSF controller uses it. The view contains all the GUI components. This phase of the lifecycle presents three view instances: new view, initial view, and post back, and each one handled separately.

Phase 2: Apply request values

In this phase each component retrieves its current state. The components are first retrieved or created from the FacesContext object, followed by their values which are typically retrieved from the request parameters, although they can also be retrieved from cookies or headers. If a component's immediate event handling property is not set to true, the values are just converted. So if the field is bound to an Integer property, the value is converted to an Integer but if value conversion fails, an error message is generated and queued in the FacesContext to be displayed in the render response phase.

Phase 3: Process validation

The first event handling of the lifecycle takes place after the apply request values phase. In this stage, each component have its values validated against the application's validation rules. The validation rules can be pre-defined or defined by the developer. Values entered by the user are compared to the validation rules. If an entered value is invalid, an error message is added to FacesContext, and the component is marked invalid. If a component is marked invalid, JSF advances to the render response phase, which will display the current view with the validation error messages.

Phase 4: Update model values

It updates the actual values of the server-side model, by updating the properties of backing beans (also known as managed beans). Only bean properties that are bound to a component's value will be updated. As this phase happens after validation, so the values copied to bean's properties are also valid.

Phase 5: Invoke application

Here the JSF controller invokes the application to handle Form submissions. The component values are converted, validated, and applied to the model objects, so we can now use them to execute the application's business logic. At this phase the next logical view for a given sequence or number of possible sequences are specified by defining a specific outcome for a successful form submission and returning that outcome. For example: on successful outcome, move the user to the next page. For this navigation, create a mapping to the successful outcome as a navigation rule in the faces-config.xml file. Once the navigation occurs, can move to the final phase of the lifecycle.

Phase 6: Render response

Render Response view with all of its components in their current state are displayed. The application utilizes common JSF GUI components like Radio List, List, Text Field, Label, Panel etc.

Comparing JSF With Existing Web Technologies

Our first question is why we need one more framework in-spite of having so many frameworks and technologies like JSP, Servlet,Struts etc. Some drawbacks of the existing technologies like JSP and Servlet are :

  • 1. Reuability Problem:- If the web project was to be developed which has 2-3 or more web forms then using technologies like JSP and Servlet the programmers have to do repetitive coding for each web form but by using JSF we can define components for form and can reuse it by simply dragging and dropping on pages.
  • 2. Security Problem:- In JSP application, programmers have to directly work with HTTP request and response objects and application was unable to map HTTP requests to component- specific event handling on server. But using JSF its components , Actionlistner tags and event handler components can easily handle request response and validations are mostly done on client side.

For example if user submits the registration form then programmer has to write the code to get the values of each element on the form into a variable for further processing. When there is a need to retrieve the data from database and then again has to code for retrieving data and showing it on the form for editing, so these becomes tedious job which can be easily handled by defining JSF components for input output operations and reusing them as per requirement.