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.

No comments:

Post a Comment