Wednesday, April 4, 2012

JSF Request Processing Lifecycle

JSF Request Processing Lifecycle:

JSF has six phases in its life cycle and below is the overview of each phase.


1. Restore View: Restores or creates a server-side component tree to represent the UI information from a client
2. Apply Request Value: Updates the server side components with request parameters from client
3. Process Validation: Performs validation and data type conversion
4. Update Model: Updates the model(bean) with the data
5. Invoke Application: Invokes application logic and performs navigation processing
6. Render response: Renders the response to the client.

Restore View


1. Restores the existing view from previous transaction or creates a new view.
2. Created view is placed in the container object known as FacesContext
3. FacesContext contains all the data pertaining to the current request that runs through the request processing life cycle.

Apply Request Value

1. Updates the server side components with request parameters from client
2. JSF runtime calls the processDecodes() on View
3. Subsequently all the processDecodes() on all components are called
4. This decodes the incoming name-value pairs and apply the value to the component.

Process Validation

1. Conversion and validation is performed on the components.
2. JSF Runtime calls processValidators() method on the View Root.
3. Propagation to processValidators() method is called on each components.
4. Data conversion happens before the validation in the same phase.
5. Any component failing conversion or validation

Update Model Values

1. Once the validation and conversion is performed with out any errors, values are now updated to the model bean.
2. JSF Runtime calls processUpdates() method on the View Root.
3. Propagation to processUpdates() method is called on each components.

Invoke Application

1. Invokes application logic and performs navigation processing
2. Any action method or action listener method is invoked.

Render Response

1. As in other phases, components encodeXX() methods are called on each component.
2. The rendered mark up language can be anything, such as HTML, WML, XML etc.,
-------------------------------------------------------------------------------------------------------------

Below are the example on request processing for few scenarios

Scenario -1: Initial Request to view register.jsf

1. User submits a request to the URL of the register page
2. The request is processes by the Faces Controller Servlet, which creates a FacesContext instance for this request and initiates a call to the lifecycle.
3. Since it’s a first request, the restore view phase will create an empty view and stores it to the FacesContext instance.
4. After view is created, since it is not a post back request , after the restore view phase, render response phase will be called for.
5. The state of the view will be stored for the future request.

Scenario-2: User Enters invalid data (Assume user does not enter data for the mandatory field)


1. User doesn’t enter the mandatory data and enters submit
2. The request is processes by the Faces Controller Servlet, which creates a FacesContext instance for this request and initiates a call to the lifecycle.
3. Since it’s a post back request, the restore view phase will retrieve the earlier created view and stores it to the FacesContext instance.
4. In the Apply Request value phase, the request parameters are processed and stored in the component tree
5. In the process Validation phase, a validation error occurs as the value was not entered for the mandatory field and the error message is added to the FacesContext.
6. Now other phase is skipped as the validation error occurred and directly goes to the last Render Response phase where the components are rendered as HTML.

Scenario-3: User Enters data valid data and submits again


1. The request is processes by the Faces Controller Servlet, which creates a FacesContext instance for this request and initiates a call to the lifecycle.
2. Since it’s a post back request, the restore view phase will retrieve the earlier created view and stores it to the FacesContext instance.
3. In the Apply Request value phase, the request parameters are processed and stored in the component tree
4. In the transition to Process Validation phase, conversion happens and since no validation occurs, the transition happens to the next phase
5. In the Update Model phase, now the values are updated to the model bean and then the action method is called in the Invoke Application phase.
6. The return string of the action method is being used by Navigation processor for deciding the next page to be rendered and the view root of the page is rendered in the Render Response phase.





No comments:

Post a Comment