Life Cycle of Java Server Faces (JSF)

Introduction

 
There are 6 phases in the life cycle of Java Server Faces (JSF).  I will use a simple JSF example to explain each phase, of the life cycle.
 
The phases are:
  1. Restore View Phase
  2. Apply Requests phase
  3. Process Validation phase
  4. Update Model Values phase
  5. Invoke Application phase
  6. Render Response phase
The diagram below (reference: image) shows the life cycle of a JSF:
 
life cycle of a JSF 
 
The JSF lifecycle handles both types of requests: Initial request and postback.
 

1. Restore View Phase

 
This is the first phase in the life cycle of JSF.  The JSF implementation builds the view of the page, adds event handlers, validates the components in the view, and saves the view in the FacesContext instance that contains the information needed to process a single request.
 
If the request is an initial request (page is requested for the first time), then an empty view is constructed and the lifecycle goes directly to render the response phase, where the component referenced by tags, are attached to the view.
 
For example, when we create a JSF application and enter the link for the first time to call the index.xhtml file, the initial request goes to the container and index.xhtml is displayed by the browser.
 
The following is the code for the index.xhtml file:
 
Code  
The output in the browser is:
 
Output  
 
If the request is a postback then a view corresponding to the page already exists.  So, during this phase, the view is restored from the state information saved on the client or server.
 
For example, when we enter a value into the TextBox in the index.xhtml page and click on the submit button.
 
index 
 

2. Apply Requests Phase

 
After the view is restored, each component extracts its new value from request parameters, using the decode method.  The value is stored locally on the component.  If the conversion fails, then an error message is generated and queued in FacesContext.  The error message will be displayed in the render response phase, along with the validation errors generated, in the process validation phase.  If, events have been queued, in this phase, then they are broadcast to interested listeners.
 
At the end of this phase, components are mapped to their new values and messages and events are queued.
 

3. Process Validation Phase

 
In this phase, the validations applied on the components of the JSP page are validated against the saved local values.  If there is any validation error, then an error message is added to the FacesContext object and the life cycle goes directly to the render response phase.
 
For example: If there were any validations applied on the input text field, then those validations will be verified against the value entered by the user.
 

4. Update Model Values Phase

 
In this phase, the local values of the tags are updated in the corresponding properties, of a managed bean class.  The value will update only for the bean properties pointed to by an input component's value attribute.  If there is any mismatch of type, then an error message is added to the FacesContext object and the life cycle goes directly to render the response phase.
 
For example, I have defined an ExampleBean class that has a textname instance variable.  The Textname variable is mapped to the TextBox value and is updated in that phase.
 
The following is the code for the ExampleBean class:
 
ExampleBean  
 

5. Invoke Application Phase

 
In this phase, all of the application level events, like submitting a form or linking to another page, are handled.  If there was an event fired from any component, then events are broadcast to interested listeners.
 
The listener processes the event and retrieves the outcome, from the component action attribute.  The listener passes this outcome to the Navigation Handler.  The Navigation Handler matches the outcome, with the configuration defined in the configuration file (web.xml), to determine the page to be displayed.  So, the response view is set to that page and control is transferred to the render response phase.
 
For example, we have set an event to move to another page, on the submit button click.  In the action attribute of the button, we have given the value "output" for the page (output.xhtml).  This event is processed and the response view is set to the output.xhtml page.
 

6. Render Response Phase

 
This is the last phase in the JSF life cycle.  In this phase, the response page is transferred to the JSP container, if the response is a JSP page.  If the request is initial, then all the components on the page are added to the component tree.  If the request is a postback and there was an error, in any of the phases, then the original page is given during this phase.  If the page has a message or a messages tag, then the queued error message will be displayed.
 
The response state is saved so that future requests can access it and it is available in the restore view phase.
 
For example, the output.xhtml content will be given and displayed on the browser, as well as its state will be saved, so that It can be used in the restore view phase.
 
Code for Output/xhtml file:
 
xhtml file 
 
The following shows a screenshot of the output.xhtml file displayed by the browser.
 
The Output.xhtml file displays the text entered into the TextBox on the index.xhtml page.
 
textbox  
 
This was a brief explanation of the JSF lifecycle.
 
I hope this article was helpful to you and thank you for reading it.