A quick Recap
In the previous tutorial, we have seen an introduction to the “JSF Lifecycle“, which is a little advanced topic in Java Server Faces JSF series you can also go to the first tutorial of this JSF series. Here we have tried to elaborate life-cycle into simple words. In the previous JSF lifecycle tutorial, we have seen the phases of JSF lifecycle. In this tutorial, we will see JSF lifecycle phases in much more detail.
Introduction to JSF Lifecycle Phases
The list of phases that we have seen in the previous tutorial:
- Restore View Phase
- Apply Request Phase
- Process Validation Phase
- Update Model Value Phase
- Invoke Application Phase
- Render Response Phase
JSF Lifecycle Phases Explanation
Considering the below-given figure. We will try to understand each of the phases of the Java Server Faces JSF Lifecycle. Each of the phases performs specific tasks that we have seen earlier. The aim of this tutorial is to understand how JSF process the user request.
JSF lifecycle starts with the faces Request which means when the user makes a request through any button click or link click then the JSF lifecycle starts with the Restore View Phase.
Restore View Phase
In this JSF lifecycle phase, JSF builds a UI component tree which is called a view. Restore View is the first thing a JSF do with the incoming request. To build a tree, JSF has its set of classes which are used to build a Component tree. We write JSF code in the JSP. JSF identify the components by the JSP name of the current view.
Now there could be two possibilities. First one is that the view is present and the second one is that the view is not present. If the view is not present JSF simply creates the new view and if the view is present JSF use the already present view.
In the previous tutorial, we have seen the definition of the view. JSF can track the status of the view by maintaining its state, and hidden HTML files maintain these states. If we have to elaborate all of the above discussion into one sentence, then we could say that “First step that JSF performs is the Creation of the restore view“.
Initial Request and Postback Request
Previously, we have seen two types of requests i.e. initial request, and post-back request. What happens when the request is initial or the request is Postback?
If the request is initial, only the empty restore view is created, and control goes to the Last phase that is the Render Response phase.
If the request is Postback, no restore view will be created. The already existed view is restored from the state information stored on the client side or the server side.
Everything that we discuss above is done in JSF lifecycle by the JSF. The user only makes the request and JSF perform these tasks. We don’t have to write the code for it. It is already implemented by the JSF framework.
After the postback request is completed in the Restore view phase. There comes Apply Request Phase of JSF lifecycle phases. In this phase, all the UI components that were created in the previous phase gets a value. That value comes from the request parameters, and this process is called decoding. In the case of error, an error message is queued in the facesContext object. These all queued messages along with process validation error messages are displayed in the last phase that is Render Response Phase.
If any component in the view has the immediate event handling property set to true then conversions and validations coupled with this component will occur in the same phase instead of occurring in the next phase.
When our web application needs to redirect to a different web application resource or want to generate a response that does not contain any JSF component, then JSF calls the FacesContext and responseComplete methods.
At the end of this phase of JSF Lifecycle phases, every UI Component present in the view is set to a new value, and the messages are queued in the facesContext.
JSf can also handle partial requests with the help of facesContext. Partial requests are processed partially.
This phase of JSF life cycle phases calls each of the registered validators. When the client makes requests, there could be a new value coming from the request. All the values coming from the request are validated against the application validation rules. Validators could be pre-defined components. And we, as a developer can also define them.
In the case of errors, remaining lifecycle phases will be aborted, and rendering of the error response message and conversion error messages will take place.
Faces Context: It is the context that contains all of the per-request state information of JSF lifecycle phases, which are related to the processing of the single JSF request and rendering of the corresponding responses will be done with it. Faces Context is modified by each phase of the request processing lifecycle.
Update Model Value
In this phase transfer of state from UI Component to the backing beans and this state transfer is done with the help of value expressions defined for the UI Components. The Transferring of the UI Components should be in context with the backing bean types. If the data can’t be converted, then the render response phase will be executed to display the errors.
If our application wants to redirect to another application then like other phases, it will call the responseComplete method of the facesContext in JSF lifecycle phases.
When we compare the apply request phase, and this phase, the baseline of both the phases is conversion so what the difference is? In the apply request phase, JSF moves values from client side HTML form control to the server side UI Component. In this phase, It is converting UI Component to Backing Beans So these two are different.
In this Phase, the application invoking occurs. This phase handles any application-level events. It deals with the event that is generated by the action of the form or by clicking on the link. It is nothing but a form to call a submit action event. Here application methods are also invoked, and it also calculates the navigation outcome.
We know that submitting a form is an application level event, and application methods are like complete response methods. But, we don’t know about the calculation of the navigation outcome. When we click on a page, it goes to another page. We only give a request to a particular page, and it redirects to another page. So how this navigation happens? When a user submits a form, then JSF handles the event and process the path of the page to redirect and show the response to the user. The process “to navigate to the page and render it” is called calculation of the navigation outcome.
If our application wants to redirect to another application then like other phases this phase will also call the responseComplete method of the facesContext.
In this JSF lifecycle phase, JSF orders the application to show what the outcome of the entire request processing lifecycle is. Rendering of the view takes place in this phase, and it stores response view state in this phase to make it available for the restore view phase. If the request is initial in this Phase, not only the values of the UI components are saved but also the other attributes of the UI Components. These attributes are associated with the presentation of the components. If the request is Postback and the error occurs then it renders the original view in this phase.
Process Event is not the phase actually, but when we see the JSF lifecycle, there are many events which are fired in case of any error or predefined exception to render the response or desired action. It handles the events which occurred during the previous phase.
In JSF there is a possibility to render or process only the part of the application. If you want to process any single component, then it is possible with the help of AJAX. It renders the whole page on user request. This request is taken into the full JSF lifecycle and renders the response. If we want to handle any specific component the partial processing could be applied to it using AJAX.
The good example for Partial rendering is the auto-suggest implemented with AJAX. In this case, It makes only the partial request from the component. And the partial rendering of the request takes place. This makes our application user-friendly and fast. These types of the requests are handled with the help of the javax.faces.context.PartialViewContextobject.
So in this tutorial, we have accomplished:
- A short recap of the previous tutorial
- Introduction to the JSF Lifecycle phases
- A brief explanation of JSF Lifecycle phases
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.