JSF Java Server Faces Request Processing Lifecycle

JSF Request Processing Lifecycle

In the previous tutorial “Introduction to Java Server Faces – JSF” we have seen the comprehensive introduction and overview of the JSF, we have also seen the Getting Started Steps of the JSF. In this tutorial, we will try to understand JSF Java Server Faces Lifecycle.

Overview of JSF Lifecycle

JSF lifecycle always remains same. It starts with the user request, when the user makes a request it arrives on the server, the first thing the server will do is to represent the view into a tree. This is a tree of UI components. The tree is then populated by values coming from the request parameters.  If request parameter does not provide the value for the element, then the values from the previous state will be used. After the tree is populated with the values, it will simply update the model components by simply copying the values from the tree; here model components are referred as server side beans. Then the response is rendered by fetching the new view if needed, and building a tree for that view which will then be populated by the values of the model components, This created view is finally seen by the user and states will be stored for the later use.

Introduction to the JSF Lifecycle

To understand the JSF life cycle we have to first understand the following terminologies.

UI Components: javax.faces.component.UI Component provides us the set of reusable interface components. With the help of javax.faces.render.Renderer Class we can combine these components to build a representable component that would be seen by the user.

View: Multiple user interface components when structured in the form of a tree is said to be a view.

Post-back request: when a form is resubmitted on the browser, which has been previously loaded into the browser, then the type of request the user/browser will make is called the postback request.

JSF Lifecycle Explained

There are certain tasks that are performed during the JSF lifecycle; we will see them in detail. JSF automatically handles the requests processing life-cycle, but there are also some ways through which we can inter-fair in the request processing lifecycle. To create a more complicated application, it becomes our need to understand the JSF life-cycle.

Common Lifecycle steps

Execute and Render are the two main steps that will be performed in any basic web application, in the execute step restoration of the application view takes place, and the values that have been coming from the request parameters will be applied to the view. These values then passed to the server been which will then be used to create a new view which will be sent back to the user. In the rendering process, a response will be generated for the user in the form of XHTML or HTML.

Standard Request Processing Java Server Faces Lifecycle

Standard request-response lifecycle is divided into following tasks.

  • Restore View
  • Apply Request
  • Process Validation
  • Update Model Value
  • Invoke Application
  • Render Response

When the inertial request is made to the server following tasks will be performed in sequence

Restore View –> Apply Request –> Render Response.

JSF Request Response Lifecycle Diagram
JSF Request Response Lifecycle Diagram

Let’s try to understand the phases one by one.

Restore View Tasks List:

  • Creation of the page view
  • Add event handlers and validators to the view.
  • Create the view within the facesContext instance
  • Make decision by request type (initial or post back)
  • In case of initial request –> create empty view –> move to render response phase
  • In case of postback request –> restore view –> move forward

Apply View Tasks List

  • Retrieve new values from request parameters
  • Load new values in the respective UI Components of the view

Process Validation Tasks list

  • Process each registered validator of the components in the tree.
  • Perform conversions for input components
  • Move to another web application resource or create a response with no UI Components

Update Model Values Tasks list

  • Set the server-side object properties by traversing the UI component tree defined in beans.
  • Move to another web application resource or create a response with no UI Components

Invoke Application Tasks List

  • Handle Application level events
  • Call the FacesContext.ResponseComplete method
  • Move to render response phase

Render Response Tasks List

  • Create a View
  • Make decision by request type (initial or post back)
  • In case of initial request –> add UI component to the component tree
  • In case of postback request –>UI components already added –> do nothing

Summary

In this tutorial, we have accomplished following things

  • A short recap of the previous tutorial
  • Overview of the Java server faces life-cycle
  • Introduction to the Java Server Faces lifecycle
  • A brief explanation of the life-cycle of java server faces

 

In the next post, you will learn about Phases of JSF Lifecycle in more detail.

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

About Author

Uzair Ahmed Khan: Technical Content Writer, Computer Software Blogger and Software Engineer Enthusiast with a keen eye on Java Technologies other Related Developments.

Leave a Reply

Your email address will not be published. Required fields are marked *