The JSF life cycle

Source: Internet
Author: User
Tags event listener

First, how JSF works (working mode)

1.JSF applications are event-driven when an event occurs (such as when a user clicks a button), the event notification is sent to the server via HTTP, and the server uses a special servlet that does facesservlet to handle the notification. Every JSF application in a Web container has its own facesservlet;

In the background, each JSF request triggers 3 things:

1) Facesservlet Create Facescontext (This object contains the servletcontext,servletrequest of the service method that the Web container passes to Facesservlet, Servletrespons object, in the processing process is mainly to modify this facescontext)

2) Facesservlet control over to lifecycle

3) Lifecycle 6 stages of processing facescontext (also known as the JSF lifecycle process)

Ii. JSF life cycle

1. Rebuild the View stage (Restore View Phase)

When a JSF page is requested, such as clicking a button or link, JSF begins rebuilding the view stage. At this stage JSF builds a view of the page, sets the event handler, validators, and saves the view in Facescontext for the components in the view. The facescontext contains all the information required to process the request, so the page elements include component tags, event handlers, converters, and validators that are exposed to facescontext. If the request is the first request, JSF produces an empty view at this stage, the life cycle enters the display answer phase, and the empty view is used when the page is returned. If the request is a return request, the view that corresponds to the page already exists, and JSF rebuilds the view with the presence of client or server-side information.

2. Apply View value stage (apply Request values Phase)

After the component tree is rebuilt, the component on each tree uses the Decode method to extract its new value from the request, which is stored in the component. If the value data conversion fails, resulting in an error associated with this component, incorporated into the context of the Facescontext, the error message is displayed at the subsequent display answer stage. If any decode method or event listener invokes the Renderresponse method of the current facescontext, JSF jumps directly to the display answer stage. If there are events at this stage, JSF broadcasts events to the listener of interest. If the app goes to another web app or the answer does not contain a JSF component, call the Facescontext.rendercomplete method. At the end of this phase, all components have been given new values, and error messages and events have been queued.

3. Process validation phase (process validations Phase)

At this stage, JSF processes the validator that is registered on all the component trees, checks the component properties that have the checksum set, and if the value is not valid JSF adds an error message to the context (Facescontext), the life cycle goes directly to the display answer stage, displays an error message, and is displayed if there is a conversion error. If any validate method or event listener invokes the Renderresponse method of the context, JSF jumps directly to the display answer stage.

4. Update model value phase (update models values Phase)

After JSF determines that the data is valid, traverse the component tree and get the corresponding value from the component set to the server object. If any updatemodels or listener calls the Renderresponse method, JSF jumps directly to the display answer stage.

5. Invoke the application phase (Invoke application Phase)

In this phase, JSF applies level events, such as table submissions or links to other pages, and events generated when the view is rebuilt are broadcast to the listener of interest, and the JSF calculation answers to the new page.

6. Show answer phase (Render Response Phase)

At this stage, if the app is a JSP page, JSF moves the control to the JSP container. If this is the first request, executing the JSP page will add the components displayed on the page to the component tree. The component displays itself when the JSP container traverses the label of the page. If the request is returned and an error occurs at another stage, the original page is displayed with an error message.

JSF provides the Phaseid class to represent the life cycle phase, which is essentially an enumeration class, often using constants:

any_phase:Any One Life cycle phase
Restore_view:Restore View Stage
apply_request_values:Apply Request Value Stage
process_validations:Process Input Validation phase
update_model_values:Updating the value stage of a model
invoke_application:Invoke the application phase
Render_response:Generate Response Phase

Third, the flowchart is as follows:

References: http://blog.sina.com.cn/s/blog_600046120100tsbv.html and https://my.oschina.net/zhaoqian/blog/71866

The JSF life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.