JSF Request Processing Life Cycle

Here we will see what happens in each phase of the JSF Request Processing Life Cycle. The first step in the JSF Request Processing Life Cycle is to restore the original view from the request or from the server itself based on the value of the ‘javax.faces.STATE_SAVING_METHOD’ in web.xml.

Let’s see what happens in each phase of  Request Processing Life Cycle when the user clicks Submit button in our example.

The six phases of the JSF application lifecycle are as follows:

  1. Restore view
  2. Apply request values
  3. Process validations
  4. Update model values
  5. Invoke application
  6. Render response

Phase 1: Restore View

In the restore view phase of the JSF lifecycle when a request from the client(browser) is send to the server, JSF framework controller examines the request and extracts the view ID, which is determined by the name of the JSP page. If the view doesn’t already exist, the JSF controller creates it. If the view already exists, it will recreate the UIView Object, which is used to render the page initially. The view stored in FacesContext contains all the GUI components. This means,the view contains not just the root view object, but also the child component that include UIInput Object corresponding to Name textbox, UICommand Object corresponding to Submit button etc. In our example, when the user clicks the submit button in the form, the view corresponding to the page already exists, so it needs only to be restored. The value attribute of the UIInput Object corresponding to the Name textbox will be null(textbox was empty) when the page was rendered first time.

Phase 2:Apply request Values

During this phase,each component in the view will search for its values in the request and set the new values to them.(Please note that in this phase the new values are set only to the component objects only, not to the backing bean instance variables.) This phase will also create an ActionEvent object corresponding to the UICommand component in the request and put in a queue to be processed later in the JSF Request Processing life cycle. In our case it create an ActionEvent corresponding to the Submit command button.

Phase 3 : Process validations

This is the phase in which the component may validate their new values. If the new value is valid and differs form the previous value a value-change event will be created and put in a queue. So in our example if the user change the name before submitting the form, a ValueChangeEvent object will be created by the UIInput component Object corresponding to the Name textbox and queued it for processing at the end of this phase.This is how the valueChangeInput method in the backing bean get invoked.
Note that if the validation fails for any of the component in the view, an error message will be queued and calls FacesContext.responseComplete method which will terminate the request processing.

Phase 4: Update Model Values

During this phase, the new validated and converted values of component will be set to the model object(backing bean Object).Please note that none of the values from the request will not set to the backing bean until all the values in the request are successfully validated and converted during the phase 2 and 3.

Phase 5: Invoke Application

Once all the values of the request has been successfully set to the backing bean the action events queued during the apply request values phase will be processed. In our case the submit buttons action method .

Phase 6: Render response

In this phase the component tree will be rendered to the client.