Friday, October 15, 2010

Jsf LIfe Cycle :

Jsf Life Cycle :
1-like servlet and jsp, jsf also have life cycle .jsf life cycle is divided into six part .if you are beginner in jsf then it paramount to learrn the jsf life cycle in depth .life cycle is vital of the jsf .here i going to explain the jsf life cycle in the depth.
2-A JavaServer Faces page is represented by a tree of UI components, called a view. When a client makes a request for the page, the life cycle starts. During the life cycle, the JavaServer Faces implementation must build the view while considering state saved from a previous submission of the page.
3-When the client submits a page, the JavaServer Faces implementation must perform following several tasks.
  1-create viewid and store state in FacesContext .
  2-assign user value in the UI component
  3-convert the user value and validate the value .

1-restore view
2-apply request value
3-process validation
4-update model value
5-invoke application
6-render response

1-Restore View :If the request is first time then it create new component tree .if the pages display already then set to their prior state.

UIViewRoot  is class who responsible to maintain the state .
package javax.faces.component;

public class UIViewRoot
    extends javax.faces.component.UIComponentBase
{
    //
    // Constructors
    //
    public UIViewRoot() { }

    //
    // Fields
    //
    public static final java.lang.String COMPONENT_TYPE;

    public static final java.lang.String COMPONENT_FAMILY;

    public static final java.lang.String UNIQUE_ID_PREFIX;

    private static javax.faces.lifecycle.Lifecycle lifecycle;

    private static final java.util.logging.Logger LOGGER;

    private int lastId;

    private boolean skipPhase;

    private boolean beforeMethodException;

    private java.util.ListIterator phaseListenerIterator;

    private java.lang.String renderKitId;

    private java.lang.String viewId;

    private javax.el.MethodExpression beforePhase;

    private javax.el.MethodExpression afterPhase;

    private java.util.List phaseListeners;

    private java.util.List events;

    private java.util.Locale locale;

    private java.lang.Object[] values;

    //
    // Methods
    //
    public java.lang.String getFamily() { }

    public java.lang.String getRenderKitId() { }

    public void setRenderKitId(java.lang.String p1) { }

    public java.lang.String getViewId() { }

    public void setViewId(java.lang.String p1) { }

    public javax.el.MethodExpression getBeforePhaseListener() { }

    public void setBeforePhaseListener(javax.el.MethodExpression p1) { }

    public javax.el.MethodExpression getAfterPhaseListener() { }

    public void setAfterPhaseListener(javax.el.MethodExpression p1) { }

    public void removePhaseListener(javax.faces.event.PhaseListener p1) { }

    public void addPhaseListener(javax.faces.event.PhaseListener p1) { }

    public void queueEvent(javax.faces.event.FacesEvent p1) { }

    private void broadcastEvents(javax.faces.context.FacesContext p1,

javax.faces.event.PhaseId p2) { }

    private void initState() { }

    private void notifyBefore(javax.faces.context.FacesContext p1,

javax.faces.event.PhaseId p2) { }

    private void notifyAfter(javax.faces.context.FacesContext p1,

javax.faces.event.PhaseId p2) { }

    public void processDecodes(javax.faces.context.FacesContext p1) { }

    public void encodeBegin(javax.faces.context.FacesContext p1) { }

    public void encodeEnd(javax.faces.context.FacesContext p1) { }

    private void notifyPhaseListeners(javax.faces.context.FacesContext p1,

javax.faces.event.PhaseId p2, boolean p3) { }

    private static javax.faces.event.PhaseEvent

createPhaseEvent(javax.faces.context.FacesContext p1, javax.faces.event.PhaseId p2) { }

    public void processValidators(javax.faces.context.FacesContext p1) { }

    public void processUpdates(javax.faces.context.FacesContext p1) { }

    public void processApplication(javax.faces.context.FacesContext p1) { }

    private void clearFacesEvents(javax.faces.context.FacesContext p1) { }

    public java.lang.String createUniqueId() { }

    public java.util.Locale getLocale() { }

    private static java.util.Locale getLocaleFromString(java.lang.String p1) { }

    private static int indexOfSet(java.lang.String p1, char[] p2, int p3) { }

    public void setLocale(java.util.Locale p1) { }

    public java.lang.Object saveState(javax.faces.context.FacesContext p1) { }

    public void restoreState(javax.faces.context.FacesContext p1, java.lang.Object p2) {

}
}
this phase is entry of the jsf life cycel and also maintain the state of component .
basically the view id is same as name of jsp page .suppose your jsp name hello.jsp then
the view id is /hello.jsp.

2-Apply Request value:
 1-in this phase fetch the parameter value from the request  scope and set in relevant components in the UIViewRoot.
  Example:outputText.setSubmittedValue("JSF");
2- the component tree is restored, each component in the tree extracts its new value from the request parameters by using its decode method.
3-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.
4- If any decode methods or event listeners called renderResponse on the current FacesContext instance, the JavaServer Faces implementation skips to the render response phase.
5-JavaServer Faces components, it can call FacesContext.responseComplete.
6-

3-Process Validations Phase 
1-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.
2-If the local value is invalid, the JavaServer Faces implementation adds an error message to the FacesContext instance, and the life cycle advances directly to the render response phase so that the page is rendered again with the error messages displayed. If there were conversion errors from the apply request values phase, the messages for these errors are also displayed.
3-If any validate methods or event listeners called renderResponse on the current FacesContext, the JavaServer Faces implementation skips to the render response phase.
4-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.
5-If events have been queued during this phase, the JavaServer Faces implementation broadcasts them to interested listeners.

4-Update Model Values Phase

1-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.
2-If any updateModels methods or any listeners called renderResponse on the current FacesContext instance, the JavaServer Faces implementation skips to the render response phase.
3-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.
4-If events have been queued during this phase, the JavaServer Faces implementation broadcasts them to interested listeners.

5-Invoke Application Phase

1-During this phase, the JavaServer Faces implementation handles any application-level events, such as submitting a form or linking to another page.
2-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.
3-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.

6-Render Response Phase

1-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.
2-If the request is a postback and errors were encountered during the apply request values phase, process validations phase, or update model values phase, the original page is rendered during this phase. If the pages contain message or messages tags, any queued error messages are displayed on the page.
3-After the content of the view is rendered, the state of the response is saved so that subsequent requests can access it and it is available to the restore view phase.

Request Processing Life Cycle Scenarios

A JavaServer Faces application supports two kinds of responses and two kinds of requests:
Faces response: A servlet response that was created by the execution of the Render Response Phase of the request processing life cycle.
Non-Faces response: A servlet response that was not created by the execution of the render response phase. An example is a JSP page that does not incorporate JavaServer Faces components.
Faces request: A servlet request that was sent from a previously generated Faces response. An example is a form submit from a JavaServer Faces user interface component, where the request URI identifies the JavaServer Faces component tree to use for processing the request.
Non-Faces request: A servlet request that was sent to an application component, such as a servlet or JSP page, rather than directed to a JavaServer Faces component tree.
These different requests and responses result in three possible life cycle scenarios that can exist for a JavaServer Faces application:
Scenario 1: Non-Faces Request Generates Faces Response
An example of this scenario occurs when clicking a hyperlink on an HTML page opens a JavaServer Faces page. To render a Faces response from a non-Faces request, an application must provide a mapping to FacesServlet, which accepts incoming requests and passes them to the life cycle implementation for processing. Identifying the Servlet for Life Cycle Processing describes how to provide a mapping to the FacesServlet. When generating a Faces response, the application must create a new view, store it in the FacesContext, acquire object references needed by the view, and call FacesContext.renderResponse, which forces immediate rendering of the view by skipping to the Render Response Phase.
Scenario 2: Faces Request Generates Non-Faces Response
Sometimes a JavaServer Faces application might need to redirect to a different web application resource or might need to generate a response that does not contain any JavaServer Faces components. In these situations, the developer must skip the rendering phase (Render Response Phase) by calling FacesContext.responseComplete. The FacesContext contains all the information associated with a particular Faces request. This method can be invoked during the Apply Request Values Phase, Process Validations Phase, or the Update Model Values Phase.
Scenario 3: Faces Request Generates Faces Response
This is the most common scenario for the life cycle of a JavaServer Faces application. It is also the scenario represented by the standard request processing life cycle described in the next section. This scenario involves a JavaServer Faces component submitting a request to a JavaServer Faces application utilizing the FacesServlet. Because the request has been handled by the JavaServer Faces implementation, no additional steps are required by the application to generate the response. All listeners, validators and converters will automatically be invoked during the appropriate phase of the standard life cycle, which the next section describes. 

How the jsf Life cycle is work in actual life ?

How the jsf Life cycle is work in actual life
Life cycle is consist of the six step .but main question is that .when the life cycle is start ?
the life cycle is depend on the request of user .the request is also divided into two part
1-initial request
2-post back
flow of the life cycle is depend on these request .

Initial request :
1-using the address of the jsf we call jsf pages .this type of the request is call initial request
2-When a user makes an initial request for a page, he or she is requesting the page for the first time.
3-If the request for the page is an initial request, the JavaServer Faces implementation creates an empty view during Restore View Phase phase and the life cycle advances to the render response phase. The empty view will be populated when the page is processed during a postback.
4-it only executes the restore view and render response phases because there is no user input or actions to process.
Post Request :
1-when user click on any link or button in jsf page this post request  created .
2-When a user executes a postback, he or she submits the form contained on a page that was previously loaded into the browser as a result of executing an initial request.
3-when the life cycle handles a postback, it executes all of the phases.
4-If the request for the page is a postback, a view corresponding to this page already exists. During this phase, the JavaServer Faces implementation restores the view by using the state information saved on the client or the server.