Six phases in the life cycle of a JSF application
- Restore View Phase
- Apply Request Values Phase and Process Events
- Process Validations Phase and Process Events
- Update Model Values Phase and Process Events
- Invoke Application Phase and Process Events
- Render Response Phase
Figure: JavaServer Faces Standard Request-Response Life Cycle
The life cycle of a JSF requests starts when the user invokes any action on the JSF page. The action could be a standard event like clicking on the button or changing the value of a text box etc. When the request is submitted to the server, the Faces Servlet gets the handle and starts processing the request.
Restore View Phase
When an event, like button click, happens the JavaServer Faces implementations starts the life cycle with the first phase i.e. the Restore View Phase. In this phase, it builds the view, attach the validators, converters and event handlers and stores in the FacesContext instance. FacesContext will have access to all these components and the information to process the request.
The UIView component forms the root of the view tree and all other components like Text Boxes, Command Button etc (UIInput), the form (UIForm) will be under the root as childs. While restoring the view, the implementation builds the exact tree, starting with the root, and attach all the validators etc., to the corresponding components in the tree.
Apply Request Values Phase
- During this phase the request values of the components will be extracted by calling the decode method of the components and the new values retrieved will be stored locally to process further validations and conventions, if any. If the conversion of the new value fails, the error message will be queued in the FacesContext instance.
- If any events attached to any component, the corresponding event listener will get invoked during this phase. If the immediate flag set on any particular component, the associated event listener, validations and conversions will get executed during this phase itself. If any validation or conversion failed during the phase, the corresponding message will be queued in the FacesContext instance and the message will gets displayed during the Render Response phase.
- If the renderResponse method called on the current FacesContext instance during this phase, the implementation skips to the Render Response phase. This will be helpful in situations like when you want to skip executing the business logic when the data entered is invalid etc.
Process Validations Phase
- In this phase all validations attached to the components will get executed. Error messages for all failed validations will gets queued in the FacesContext instance and will get displayed during the Render Response phase.
- Any events that are queued for this phase will be processed and the corresponding event listener gets invoked. Similar to the Apply Request Values Phase, if any method during this phase (like validate, event handler methods) call the renderResponse method on the FacesContext instance, the executions will skips to the Render Response phase. This is true with the next phase as well.
- If you want to skip the current execution during any phase and render a different page (no jsf page) on a different web application, you can simply call the responseComplete method on the FaceContext instance.
Update Model Values Phase
- If the execution of the applications successfully completes the third phase means that the request data is valid and can be assigned to the model. So the data will be applied to the model and if any conversion fails, the application will skips to Render Response phase.
- Again, if any updateModes method or event listener method calls the renderResponse method, the executions skips to the last phase. Also any events registered will be broadcasted to the corresponding event listeners.
Invoke Application Phase
- This is the phase where you execute the actual business logic. The JavaServer Faces controller will invoke the application to to handle the form submissions. The component values validated, converted and applied to the model and you can use the model in your business logic.
- You can specify the next view to be rendered in this phase by returning the corresponding view mapping. See the navigation rules declaration in faces-config.xml file.
Render Response Phase
- This is the last phase of the JSF page execution life cycle. In this phase the result view will be rendered with the generated components and their corresponding values existing with the model.