JSF life cycle

1. How JSF works (how it works)

  1. JSF applications are event-driven. When an event occurs (for example, a user clicks a button), the event notification is sent to the server through HTTP, and the server uses a special servlet called FacesServlet to process the notification. Each jsf application in the web container has it's own FacesServlet;

Behind the scenes, every jsf request triggers 3 things:

  1) FacesServlet creates FacesContext (this object contains the ServletContext, ServletRequest, ServletRespons objects passed by the Web container to the service method of FacesServlet, which is mainly to modify this FacesContext during processing)

  2) FacesServlet gives control to Lifecycle

  3) Lifecycle processes FacesContext in 6 stages (that is, jsf life cycle process)

Second, the JSF life cycle

  1. Restore View Phase

When a JSF page is requested, such as a button or link click, JSF starts the rebuilding view phase. At this stage, JSF builds the view of the page, sets event handlers and validators for the components in the view, and saves the view in the FacesContext. The FacesContext contains all the information for processing the request, so page elements including component tags, event handlers, converters, and validators must contact the FacesContext. If the request is the first request, JSF generates an empty view at this stage, and the life cycle enters the display response stage. This empty view will be used when the page returns. If the request is a returned request, and the view corresponding to this page already exists, JSF rebuilds the view with the information that exists on the client or server side.

  2. Apply Request Values ​​Phase

After the component tree is rebuilt, each component in the tree uses the decode method to extract its new value from the request, and this value is stored in the component. If the value data conversion fails, an error associated with this component is generated, merged into the context of the FacesContext, and the error message is displayed in the subsequent display response phase. If any decode method or event listener calls the renderResponse method of the current FacesContext, JSF jumps directly to the display response phase. If an event occurs during this phase, JSF broadcasts the event to interested listeners. If the application goes to another web application at this time or the response does not contain a JSF component, the FacesContext.renderComplete method is called. At the end of this phase, all components have got new values, and error messages and events have been queued.

  3. Process Validations Phase

At this stage, JSF processes all validators registered on the component tree, and checks the component properties that have been checked. If the value is invalid, JSF adds an error message to the context (FacesContext), and the life cycle directly enters the display response stage and displays the error message. , also displayed if there are conversion errors. If any validate method or event listener calls the context's renderResponse method, JSF jumps directly to the display response phase.

  4. Update Model Values ​​Phase

After JSF determines that the data is valid, it traverses the component tree, obtains the corresponding value from the component and sets it on the server object. If any updateModels or listeners call the renderResponse method, JSF jumps directly to the display response phase.

  5. Invoke Application Phase

At this stage, in addition to application-level events, such as: form submission or links to other pages, etc.; events generated when the view is rebuilt are broadcast to interested listeners, and JSF calculates the response to the new page.

  6. Render Response Phase

At this stage, if the application is a JSP page, JSF will transfer control to the JSP container. If it is the first request, executing the JSP page will add the components displayed on the page to the component tree. Components display themselves as the JSP container traverses the page's tags. If it is a returned request and an error occurs at other stages, the original page is displayed and the error message is displayed.

JSF provides the PhaseId class to represent the life cycle stage, which is essentially an enumeration class, often used constants:

ANY_PHASE: any lifecycle phase
RESTORE_VIEW: restore view phase
APPLY_REQUEST_VALUES: apply request value phase
PROCESS_VALIDATIONS: process input validation phase
UPDATE_MODEL_VALUES: update model values ​​phase
INVOKE_APPLICATION: call application phase
RENDER_RESPONSE: generate response phase

3. The flow chart is as follows:

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

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324611343&siteId=291194637