Monday Sep 01

Key Concepts

JSF 2 Lifecycle

PDFPrintE-mail
Wednesday, 15 June 2011 09:02
AddThis Social Bookmark Button

JSF  2 Lifecycle 

The JSF  application lifecycle contains six phases : 

Index

Phases

Processing
1 Restore View
2 Apply requests Process events
3 Process Validations
4 Update model values Process events
5 Invoke applications Process events
6 Render response

JSF  2 Lifecycle diagram : 

JSF 2 Lifecycle diagram - Java tutorial

Restore view

RestoreView is the first phase in the JSF  lifecycle. This phase is used for constructing view to display in the front end. Every view has it's own view id and it is stored in the FacesContext's session object. JSF  View is collection of components associated with its current state. There is two types of state saving mechanism,

  1. Server (default)
  2. Client

Default value is server. If you specify state saving method as server, the state of each components will be stored in the server. If it is client, it will be stored in the client side as hidden variables. This value is configured using javax.faces.STATE_SAVING_METHOD parameter name in your web.xml context params as follows:

jsf restore view state saving configuration in web.xml

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

Apply Requests

After the component tree is restored, each component in the tree extracts its new value from the request parameters by using its decode method. The value is then stored locally on the component. If the conversion of the value fails, an error message associated with the component is generated and queued on FacesContext. This message will be displayed during the render response phase, along with any validation errors resulting from the process validations phase.

Process Validations

During this phase, the JavaServer Faces implementation processes all validators registered on the components in the tree. It examines the component attributes that specify the rules for the validation and compares these rules to the local value stored for the component.

Update Model Values 

After the JavaServer Faces implementation determines that the data is valid, it can walk the component tree and set the corresponding server-side object properties to the components' local values. The JavaServer Faces implementation will update only the bean properties pointed at by an input component's value attribute.

If the local data cannot be converted to the types specified by the bean properties, the life cycle advances directly to the render response phase so that the page is rerendered with errors displayed. This is similar to what happens with validation errors.

Invoke Applications

During this phase, the JavaServer Faces implementation handles any application-level events, such as submitting a form or linking to another page.

At this point, if the application needs to redirect to a different web application resource or generate a response that does not contain any JavaServer Faces components, it can call FacesContext.responseComplete.

If the view being processed was reconstructed from state information from a previous request and if a component has fired an event, these events are broadcast to interested listeners.

Render Response 

During this phase, the JavaServer Faces implementation delegates authority for rendering the page to the JSP container if the application is using JSP pages. If this is an initial request, the components represented on the page will be added to the component tree as the JSP container executes the page. If this is not an initial request, the components are already added to the tree so they needn't be added again. In either case, the components will render themselves as the JSP container traverses the tags in the page.

Conclusion

If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on

Tags: tree, view, process, javaserver, components, faces, component, stored, response

Comments  

 
+6 #4 Kunal 2013-07-07 13:21
What is the Process Events (is it a phase?)
after phase 2, 4 and 5. What happens exactly there? And in case does the response get completed after any of these 3 phases? Hope not asking too many qns? :lol:
Quote
 
 
+3 #3 Lokesh guru 2013-04-05 06:07
Good article .Helped a lot..!
Quote
 
 
+3 #2 Mohammed Amr 2012-10-20 10:48
I think it's very simple and it's not discuss things in details such as where is the component set it's request value initially before it convert and validate that value.
I hope to review this document to be more advanced.
Quote
 
 
+8 #1 Jitendra 2012-04-22 05:51
Very good article about JSF life cycle. Keep it up.
Quote
 

Add comment


Security code
Refresh

Java Tutorial on Facebook