Following are the steps involved in JSF page life cycle.
The life cycle handles both Initial and Postback event.(For a page having input text field with value coming fron a bean say #{bean.name}, then Initial request is the request that the browser does in order to display the page with the ${bean.name} tag and Postback happens when the browser posts some or all page values and then the same page that was posted in the first place is returned to the client). When the life cycle handles an initial request, it only executes the restore view and render response phases because there is no user input or actions to process. Conversely, when the life cycle handles a postback, it executes all of the phases.
1) Restore View Phase: Begins when a link or button is clicked.the JSF implementation builds the view of the page, wires event handlers and validators to components in the view, and saves the view in the
For Initial request JSF creates an empty view and phase changes to Render response phase. The empty view will be used when page is processed during postback request.
For postback request, a view corresponding to page already exists. The JSF implementation restore the view by using state information saved on client or server.
Phase Result: Component Tree created/restored.
2) Apply Request value phase: Each component in the tree extract its value from decode method. The decode method looks like:
public void decode(FacesContext context, UIComponent component) {
/* Grab the request map from the external context */
Map requestMap = context.getExternalContext().getRequestParameterMap();
/* Get client ID, use client ID to grab value from parameters */
String clientId = component.getClientId(context);
String value = (String) requestMap.get(clientId);
FieldComponent fieldComponent = (FieldComponent)component;
/* Set the submitted value */
((UIInput)component).setSubmittedValue(value);
}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
Phase Result: The components are set to their new values, and messages and events have been queued.
3) Process Validation Phase: All the validators registered on the component are processed in this phase. These rules are implemented on the local values stored. If local value is invalid, then error message is added to facescontext and phase changes to render phase. If events have been queued during this phase, the JavaServer Faces implementation broadcasts them to interested listeners.
Phase Result: Ensure that the data is valid.
4) Update Model Value Phase: This phase set the object properties to local values. JSF implementation updates only the bean properties pointed at by input component value attribute. If local data can't be converted to type specified by bean propeties and error message is added to facescontext and phase chages to render phase.
Phase Result: The properties of the bean are set to the local variable of the UI component.
5) Invoke Application Phase: This phase deals with invoking application level events like submitting forms or redirecting to other page. While processing redirecting event, if any, a default ActionListener implementation retrive the outcome from component action attribute and passed the outcome to default NavigationHandler. The JSF implementation then set view to new page as per the navigation rule.Otherwise, JSF implementation passes control to render phase.
Phase Result: Change of view in case of any navigation or application's invoke.
6) Render Response Phase: The JSP container is now assigned the authority to render the jsp pages.
If the request is Initial request, the components represented on the page will be added to the component tree as the JSP container executes the page.
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.
The life cycle handles both Initial and Postback event.(For a page having input text field with value coming fron a bean say #{bean.name}, then Initial request is the request that the browser does in order to display the page with the ${bean.name} tag and Postback happens when the browser posts some or all page values and then the same page that was posted in the first place is returned to the client). When the life cycle handles an initial request, it only executes the restore view and render response phases because there is no user input or actions to process. Conversely, when the life cycle handles a postback, it executes all of the phases.
1) Restore View Phase: Begins when a link or button is clicked.the JSF implementation builds the view of the page, wires event handlers and validators to components in the view, and saves the view in the
FacesContext
instance. For Initial request JSF creates an empty view and phase changes to Render response phase. The empty view will be used when page is processed during postback request.
For postback request, a view corresponding to page already exists. The JSF implementation restore the view by using state information saved on client or server.
Phase Result: Component Tree created/restored.
2) Apply Request value phase: Each component in the tree extract its value from decode method. The decode method looks like:
public void decode(FacesContext context, UIComponent component) {
/* Grab the request map from the external context */
Map requestMap = context.getExternalContext().getRequestParameterMap();
/* Get client ID, use client ID to grab value from parameters */
String clientId = component.getClientId(context);
String value = (String) requestMap.get(clientId);
FieldComponent fieldComponent = (FieldComponent)component;
/* Set the submitted value */
((UIInput)component).setSubmittedValue(value);
}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. Phase Result: The components are set to their new values, and messages and events have been queued.
3) Process Validation Phase: All the validators registered on the component are processed in this phase. These rules are implemented on the local values stored. If local value is invalid, then error message is added to facescontext and phase changes to render phase. If events have been queued during this phase, the JavaServer Faces implementation broadcasts them to interested listeners.
Phase Result: Ensure that the data is valid.
4) Update Model Value Phase: This phase set the object properties to local values. JSF implementation updates only the bean properties pointed at by input component value attribute. If local data can't be converted to type specified by bean propeties and error message is added to facescontext and phase chages to render phase.
Phase Result: The properties of the bean are set to the local variable of the UI component.
5) Invoke Application Phase: This phase deals with invoking application level events like submitting forms or redirecting to other page. While processing redirecting event, if any, a default ActionListener implementation retrive the outcome from component action attribute and passed the outcome to default NavigationHandler. The JSF implementation then set view to new page as per the navigation rule.Otherwise, JSF implementation passes control to render phase.
Phase Result: Change of view in case of any navigation or application's invoke.
6) Render Response Phase: The JSP container is now assigned the authority to render the jsp pages.
If the request is Initial request, the components represented on the page will be added to the component tree as the JSP container executes the page.
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.
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