First, JSF life cycle
JSF is event-driven. The JSF life cycle is divided into two main phases: the execution phase and the rendering phase.
1. Implementation phase
It is divided into six stages:
When a client requests a JavaServer faces page, the JavaServer faces implementation begins the recovery view phase. At this stage, JSF builds the components in the view as a view of the request page, linear event handlers, and validators, and saves the view in the Facescontext instance. If the request to the page is a postback, then the view corresponding to the page already exists in the Facescontext instance. At this stage, JavaServer faces implements the restore view by using state information that is saved on the client or server.
- Apply Request Value Stage
During this phase, the component tree is restored during postback requests. A component tree is a collection of form elements. Each component in the tree decode(processDecodes())
extracts its new value from the request parameter by using its method. After that, the value is stored locally on each component.
1. If any decoding method or event listener calls the Renderresponse method on the current Facescontext instance, the JavaServer faces implementation skips the render response phase.
2. If any event is queued at this stage, the JavaServer faces implementation broadcasts the event to an interested listener.
3. You can call a method if your application needs to redirect to another Web application resource or to generate a response that does not contain any javaserver faces components FacesContext.responseComplete()
.
4. If the current request is recognized as a partial request, the partial context is retrieved from Facescontext and a partial processing method is applied.
At this stage, JavaServer faces validate()
handles all validators registered on the component by using its methods. It examines the component properties of the specified validation rule and compares those rules with the local values stored for the component. JavaServer faces also completed the conversion of the immediate
input component without setting the property to true
.
1. The renderResponse
JavaServer faces implementation skips the render response phase if any of the authentication methods or event listeners are facescontext the method at the current time.
2. You can call a method if your application needs to redirect to a different Web application resource or to generate a response that does not contain any javaserver faces components FacesContext.responseComplete
.
3. If events are queued at this stage, the JavaServer faces implementation broadcasts them to an interested listener.
4. If the current request is recognized as a partial request, the partial context is retrieved from Facescontext and a partial processing method is applied.
After making sure that the data is valid, it traverses the component tree and sets the corresponding server-side object property to the component's local value. The JavaServer faces implementation only updates the properties of the input component to point to the value
bean
property. If the local data cannot be converted to bean
the type specified by the property, the life cycle advances directly to the render response stage to re-render the page and display an error.
- Invoke the application phase
At this stage, JSF handles application-level events, such as submitting a form or linking to another page. Now, if your application needs to redirect to another Web application resource or generate a response that does not contain any JSF components, you can call the FacesContext.responseComplete()
method. After that, the JavaServer faces implementation transfers control to the render response stage.
This is the final phase of the JSF life cycle. At this stage, JSF builds the view and delegates the permissions to the appropriate resources to render the page.
1. If this is the initial request, the component represented on the page will be added to the component tree.
2. If this is not the initial request, the component has been added to the tree and does not need to be added.
3. If the request is a response and an error is encountered during the process validation phase or the update model value phase during the application request value phase, the original page is rendered again at this stage.
If the page contains h:message
or h:messages
labels, any queued error messages are displayed on the page. After the contents of the view are rendered, the state of the response is saved so that subsequent requests can access it. The restore view stage can use the saved state.
2. Rendering phase
At this stage, the requested view acts as a response to the client browser. View rendering is the process of generating output in HTML or XHTML. So the user can see it in the browser.
Take the following steps during the rendering process.
- The
index.xhtml
application is compiled when the client makes an initial request to the Web page.
- The application is executed after compilation, and a new component tree is built for the application and placed in the Facescontext.
EL
populates a component tree with a component represented by an expression and its associated managed bean
property.
- Based on the component tree. A new view is created.
- The view is rendered as a response to the requesting client.
- The component tree is automatically destroyed.
- In subsequent requests, the component tree is rebuilt and the saved state is applied.
Original reference: Https://www.yiibai.com/jsf/jsf-life-cycle.html
First, Facelets
1.Facelets is an alternative presentation layer technique for building JSF applications. Provides a powerful templating system that allows you to use HTML-style templates to define the performance layer of JSF, reducing the code that is redundant when components are integrated into the presentation layer, without needing a web container.
2. Advantages
The advantage of using Facelets is that component composition and template framework, the core of which is the component, not the servlets output. But the use of Facelets is also somewhat inadequate. One of them is that there is very little support for the Facelets IDE.
Using the Facelets API is natural and intuitive.
If you want to use the new JSF component library, you must have a Facelets tag library that exposes the library.
The life cycle of JSF and Facelets