java week 6

1. The workflow and life cycle of jsf

JSF is a web application and also follows the request/corresponding architecture, which is completely similar to the life cycle of JSP: the client sends an HTTP request, the server processes the request, and sends back an HTTP response to the client. The entire JSP life cycle Finish.

JSF refines the request/response life cycle into more stages, enabling more complex UI component models.

1. JSF divides user requests into two types:
1. Initial request: When a user sends a request directly to a page, the request does not have any additional parameter information. 2. Submit the form: When the submit button of the form is clicked or the hyperlink is submitted, the submit form request will occur.
2. The six life cycles of JSF:

2.1. Restoring the view phase:

When a client sends a request to a JSF page, such as when a hyperlink or button is clicked, JSF begins the restore view phase.

At this stage, JSF will create a corresponding view for the page, connect event listeners, input validators, etc. to the UI components contained in the page, and use the FacesContext instance to save the view object. At this point, all application-related components, including UI, event handlers, converters, and input validators can access the FacesContext instance.

In case of initial request: JSF will create a new view object at that stage and advance the lifecycle stage directly to the production response stage.

If the form is submitted: the view corresponding to the page already exists, and JSF will use the client-side or server-side information to restore the view.

2.2. Application request value stage:

When the view is restored, each component will call its decode method to extract the new parameter value from the request parameter and save it on the local component (the type conversion needs to be performed before saving. If the value conversion fails, JSF will use the FacesContext to save component-related error messages and put messages into message pairs, which will wait until the output response phase is processed several times).

If any component's decode method or event listener calls the FacesContext's renderResponse method, then JSF will proceed directly to the generating response phase.

If a component of the page has immediate="true" set, this stage also handles validations, transitions and events related to these components.

At this stage, the application can redirect to other resources, or generate a response that does not contain the Java Server Faces component. If this processing is required, the developer must call Facescontext. responseComplete to skip generating the response phase.

After this phase, all components are set to their new values, and all messages and events are put into the queue.

2.3. Processing input verification stage:

To process the input validator registered by the UI component, JSF will compare the local value on each UI component with the corresponding input validation rule. If the local value is invalid, JSF will add the corresponding error message to the FacesContext instance, and JSF will The life cycle will also be directly advanced to the stage of generating the response, and the display page can display the error information of input validation through <h:message> or <h:messages>.

In this phase, any validator methods and event listeners call the renderResponse method of the current FacesContext instance, and the application life cycle will proceed directly to the generation response phase.

If you need to redirect to another resource at this stage, or generate a response that does not contain a Java Server Faces component, the developer can call FacesContext.responseComplete to jump to the response generation stage.

2.4. Update the value phase of the model:

After passing the input validation, use the local value of the UI component to update the managed bean bound to it (transmit the data entered by the user to the managed bean on the server side), and JSF will only update the managed bean bound to the input component.

If it cannot be updated, an error message will be generated, and the life cycle will directly advance to the stage of generating the response. You can use <h:message> or <h:messages> to display the error message.

During this phase, if any updateModels method or event listener calls the renderResponse method of the current FacesContext instance, the lifecycle will proceed directly to the generating response phase.

If you need to redirect to another resource at this stage, or generate a response that does not contain a Java Server Faces component, the developer can call FacesContext.responseComplete to jump to the response generation stage.

2.5. Call the application stage:

During this phase, JSF will handle application-level events, such as submitting forms or linking to other pages.

If you need to redirect to another resource at this stage, or generate a response that does not contain a Java Server Faces component, the developer can call FacesContext.responseComplete to jump to the response generation stage.

2.6. Generate response stage:

In this phase, the application is ready to output the response to the client. If a JSP page is used, JSF will call the JSP container to process the response. If it is the initial request, the components on the page will be added to the component tree by the JSP container; if it is not the initial request, all components have already been added to the component tree, so there is no need to add them again.

If an error is encountered during the form submit phase, and during the apply request value phase, the processing phase, or the update model phase, the application will choose to generate the initial page.

After the response is generated, the application's response state is also saved.

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
Reprinted: https://blog.csdn.net/ustczyy/article/details/10932785
2. Workflow and life cycle of Facelets

Facelets is a lightweight page declaration language for building JSF (JavaServer Faces) views using HTML styles.

It includes the following features:

  • It uses XHTML to create web pages.
  • In addition to supporting JavaServer Faces and the JSTL tag library, it also supports the Facelets tag library.
  • It supports Expression Language (EL).
  • It is a template that uses components and pages.

advantage

  • It supports code reusability through templates and composite components.
  • It provides functional extensibility of components and other server-side objects through customization.
  • Compile time is faster
  • It validates the expression language at compile time.
  • High-performance rendering capabilities.

JSF (JavaServer Faces) technology supports various tag libraries to add components to web pages. To support the JavaServer Faces tag library mechanism, Facelets use XML namespace declarations.

The following table shows the tag libraries supported by Facelets.

Tag library HATE prefix Example content
JSF Facelets Tag Library http://xmlns.jcp.org/jsf/facelets ui: ui:componentui:insert Template tags
JSF HTML tag library http://xmlns.jcp.org/jsf/html h: h:head,h:body JavaServer Faces component markup for all UI component objects
JSF core tag library http://xmlns.jcp.org/jsf/core f: f:actionListenerf:attribute JSF tags are independent of any custom actions for a particular rendering toolkit
Pass element tag library http://xmlns.jcp.org/jsf jsf: jsf:id Tags that support HTML5 friendly markup
pass attribute tag library http://xmlns.jcp.org/jsf/passthrough p: p:type Tags that support HTML5 friendly markup
Composite Component Tag Library http://xmlns.jcp.org/jsf/composite cc: cc:interface Tags that support composite components
JSTL Core Tag Library http://xmlns.jcp.org/jsp/jstl/core c: c:forEachc:catch JSTL 1.2 core tags
JSTL function tag library http://xmlns.jcp.org/jsp/jstl/functions fn: fn:toUpperCasefn:toLowerCase JSTL 1.2 function tags

Lifecycle of a Facelets application

The JSF specification defines the life cycle of a JavaServer Faces application. The following steps describe the process for a  Facelets  -based application.

  1. The lifecycle is started when the client Faceletsmakes a new request with the created web page. JSF creates a new component tree or javax.faces.component.UIViewRootputs it in FacesContex.

  2. Views can populate components for rendering UIViewRootif available .Facelets

  3. The newly created view is rendered in response to the client.

  4. When rendering, store the state of this view for the next request. Stores the state of input components and form data.

  5. Clients can interact with a view and request another view from a JSF application. At this point, the saved view is restored from the stored state.

  6. Restoring the view goes through the JSF lifecycle again, and if there are no validation issues, and no action is triggered, it will eventually generate a new view or re-render the current view.

  7. If the same view is requested, the stored view is rendered again.

  8. Continue if a new view is requested.

  9. Get the new view as a response to the client.

Guess you like

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