jsf刷新页面_JSF页面生命周期管理

jsf刷新页面

When the user requests for a page, the lifecycle of JSF begins. JavaServer faces builds the current view from the previously saved state which is infact from the state of the submission of the previous page. The framework performs certain tasks like validating the inputs fields of the page, generating response and so on.

当用户请求页面时,JSF的生命周期开始。 JavaServer faces从先前保存的状态构建当前视图,而先前保存的状态实际上是从上一页提交的状态开始的。 该框架执行某些任务,例如验证页面的输入字段,生成响应等。

JSF Life cycle involves six phases namely

JSF生命周期涉及六个阶段,即

  1. Restore view phase

    恢复视图阶段
  2. Apply request values phase

    应用请求值阶段
  3. Process validations phase

    流程验证阶段
  4. Update model values phase

    更新模型值阶段
  5. Invoke application phase

    调用应用程序阶段
  6. Render response phase

    渲染响应阶段

Lets look in detail to each of these phases.

让我们详细查看每个阶段。

  1. Restore view phase: Whenever a request arrives, this is the first phase that gets initiated. When the request arrives – that is, when a button or a link is clicked, jsf builds the view, prepares the event handlers, validates the ui components and saves the view in a faces context. This faces context will have all the previous request’s information to process the current request. If the request is an initial one, the JSF creates an empty view and displays it to the user during a postback. If the postback view already exists, the information is popped out from the saved state.

    恢复视图阶段 :每当请求到达时,这就是启动的第一个阶段。 当请求到达时-即单击按钮或链接时,jsf生成视图,准备事件处理程序,验证ui组件并将视图保存在faces上下文中。 该面Kong上下文将具有所有先前请求的信息来处理当前请求。 如果请求是最初的请求,那么JSF将创建一个空视图并在回发期间将其显示给用户。 如果回发视图已经存在,则该信息将从已保存状态弹出。
  2. Apply request values: Once the components are restored from in the first phase, each component extracts its new value which might have been changed from the previous state through a decode method. The extracted value is then stored locally(native data type) along with the component. If the process of decoding fails, an error message is generated and stored in faces context for further action. If the components have their immediate JSF attributes set to true in the page then the events, validation and conversion related to the page will be processed. If any decode method calls the render response phase then the the control is redirected to render response phase itself.
    If the application needs to redirect to a different application it can call FacesContext.responseComplete. By the end of this phase the new values will be set and messages, events will be queued.

    应用请求值 :在第一阶段从组件还原后,每个组件都会提取其新值,该新值可能已通过解码方法从先前状态更改了。 然后将提取的值与组件一起存储在本地(本机数据类型)。 如果解码过程失败,则会生成错误消息并将其存储在face上下文中,以采取进一步的措施。 如果组件在页面中将其直接JSF属性设置为true,则将处理与页面相关的事件,验证和转换。 如果有任何解码方法调用渲染响应阶段,则控件将重定向到渲染响应阶段本身。
    如果应用程序需要重定向到其他应用程序,则可以调用FacesContext.responseComplete。 在此阶段结束时,将设置新值,并将消息,事件排队。
  3. Process validations phase: During this phase all the field related validations are processed and added to the component tree. Each and every rule is examined and compared with the values stored on the local component. If the values on the local component is invalid, error messages are registered in faces context and the same page is rendered again with the error message there by navigating to the render response phase.

    流程验证阶段 :在此阶段中,将处理所有与字段相关的验证,并将其添加到组件树中。 将检查每条规则,并将其与本地组件上存储的值进行比较。 如果本地组件上的值无效,则会在faces上下文中注册错误消息,并通过导航到渲染响应阶段,再次在该页面上显示该错误消息。
  4. Update model values phase: Since the Data validations are done in the validation phase now the corresponding server object properties are set and stored in the local components. The bean properties are updated to the corresponding input component attributes.

    更新模型值阶段 :由于数据验证是在验证阶段完成的,因此现在已设置了相应的服务器对象属性并将其存储在本地组件中。 Bean属性将更新为相应的输入组件属性。
  5. Invoke application phase: The events like submitting a form, click of a link are handled in this phase. If the processed views are reconstructed based on state information of previous request and if the event is fired it will be broadcasted to interested listeners. If redirection to different web application is invoked again the transition to response render phase takes place.

    调用应用程序阶段 :在此阶段处理诸如提交表单,单击链接之类的事件。 如果基于先前请求的状态信息重构了处理后的视图,并且如果触发了事件,则将其广播给感兴趣的侦听器。 如果再次调用重定向到另一个Web应用程序,则将过渡到响应呈现阶段。
  6. Render response phase: The Java Server Faces first checks whether it is a jsp page and if yes, the control is transferred to JSP Container. If the request is an initial one, the page is added to the component tree first and stored. If it is not an initial request, the component will already be stored and hence it just gets rendered. In either case the components will render themselves as the JSP container traverses the tags in the page. If any errors are encountered during this process the relevant messages are displayed. After the view is rendered the content will be stored so that when the same request arrives, it can be accessed and also be available for restore view phase.

    渲染响应阶段 :Java Server Faces首先检查它是否为jsp页面,如果是,则将控件转移到JSP容器。 如果该请求是初始请求,则该页面将首先添加到组件树并进行存储。 如果不是初始请求,则该组件将已经存储,因此将被渲染。 无论哪种情况,组件都将在JSP容器遍历页面中的标记时呈现。 如果在此过程中遇到任何错误,则会显示相关消息。 呈现视图后,将存储内容,以便在同一请求到达时可以对其进行访问,并且还可以用于还原视图阶段。

Lets consider an example to understand each phase.

让我们考虑一个例子来理解每个阶段。

A page is rendered where user can enter the car details like car id, name, color, model etc and then the entered details will be displayed in the browser.

呈现页面,用户可以在其中输入汽车详细信息,例如汽车ID,名称,颜色,型号等,然后输入的详细信息将显示在浏览器中。

The addDetailsPage.xhtml contains all the fields of the car into which the user can enter the details. The code for addDetailsPage is as follows.

addDetailsPage.xhtml包含用户可在其中输入详细信息的汽车的所有字段。 addDetailsPage的代码如下。

addDetailsPage.xhtml

addDetailsPage.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"
      xmlns:h="https://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid columns="3">
                <h:outputLabel>Car Id:</h:outputLabel>
                <h:inputText id="cid" value="#{car.id}"></h:inputText>
            <br /> <br />
            <h:outputLabel>Car Name:</h:outputLabel>
            <h:inputText id="cname" value="#{car.cname}"></h:inputText>
            <br /> <br />
            <h:outputLabel for="color">Car color:</h:outputLabel>
            <h:inputText id="color" value="#{car.color}"></h:inputText>
            <br /> <br />
            <h:commandButton value="Reset"  action="#{car.clearAll}"></h:commandButton>
            </h:panelGrid>
        </h:form>
        </h:body>
</html>

Create a Managed Bean Car.java as shown below.

如下所示创建一个Managed Bean Car.java。

package com.journaldev.jsf.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Car {
    private String cname;
    private String Id;
    private String color;
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getCname() {
                return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }


    public String getId() {
        return Id;
    }

    public void setId(String Id) {
        this.Id = Id;
    }
    
    public void clearAll(){
      this.Id = "";
      this.cname = "";
      this.color="";
  }    
}

Assume that the user enters https://localhost:8080/HelloWorld/faces/addDetailsPage.xhtml in the browser. Below are the execution steps.

假设用户在浏览器中输入https:// localhost:8080 / HelloWorld / faces / addDetailsPage.xhtml 。 以下是执行步骤。

Step 1: Since it is a initial request there is nothing to restore hence an empty view is created which is posted back.

步骤1 :由于这是初始请求,因此无法还原,因此创建了一个空视图并将其发回。

Step 2: The values entered by the user for car details are set here like car id=3, name=”Alto” and color=”blue”.

步骤2 :在此设置用户输入的汽车详细信息值,例如汽车ID = 3,名称=“ Alto”​​和颜色=“ blue”。

Step 3: Here the validations related to car id, name and color are checked. Since there are no validations set no processing is done here. Suppose, we make car id as mandatory and submit the page with the car id as null, the validator generates error messages and proceeds to the last step which is the render response.

步骤3 :此处检查与汽车ID,名称和颜色有关的验证。 由于没有设置验证,因此此处不进行任何处理。 假设我们将car id设为必选,并提交car id为null的页面,验证器将生成错误消息,并继续执行最后一步,即渲染响应。

Step 4: In the model phase the values from the page are extracted and inserted into the corresponding variables in the car bean. Clear all method is mapped and functionality of clear all is accomplished here if requested.

步骤4 :在模型阶段,将提取页面中的值并将其插入到car bean中的相应变量中。 清除全部方法已映射,如果需要,清除全部功能在此处完成。

Step 5: Here the click of reset button is handled. The view to be rendered is constructed here. This is the result of triggering a functionality in step 4. For example, when we clear the fields, the bean method is executed and the new view is constructed with all the field values set to null.

第5步 :此处处理了重置按钮的单击。 在此处构造要渲染的视图。 这是在步骤4中触发功能的结果。例如,当我们清除字段时,将执行bean方法,并使用所有字段值都设置为null的新视图。

Step 6: The view page addDetailsPage.xhtml with the final result and field values is rendered. In this example it is a page with all the fields set to blank values.

步骤6 :呈现具有最终结果和字段值的视图页面addDetailsPage.xhtml。 在此示例中,它是一个页面,其中所有字段都设置为空白值。

That’s all for the JSF Page Lifecycle Management, we will look into more of JSF features in coming posts.

这就是JSF Page Lifecycle Management的全部内容,我们将在以后的文章中探讨更多JSF功能。

翻译自: https://www.journaldev.com/6888/jsf-page-lifecycle-management

jsf刷新页面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值