【正文】
connects the event handler function to the ponent. The view is saved in a FacesContext object. JacesContext storage status information, JSF need to use this information for the current request to manage the status of the GUI ponent. FacesContext stores the view in its viewRoot property。 viewRoot contains all the JSF ponents that correspond to the current view ID.For the initial view (the first load page), JSF creates an empty view. Fill this empty view when you are dealing with the JSP page. After filling the initial view, JSF directly into the display response phase. For postback, a page that has been returned to a previous visit, the view that corresponds to the page already exists, so just restore it. In this case, the JSF uses the state information of the existing view to re construct its state. The goal of applying the requested value phase is to obtain the current state of each ponent. First, you have to get or create a ponent from the FacesContext object, and then get the value of the object. Component values are often retrieved from the request parameters, but can also be retrieved from the cookie or request header. For many ponents, the value of the request parameter is stored in the submittedValue of the ponent. If the direct event processing property of the ponent is true, the value is converted to the correct type and is checked (further in the next phase). Then, the converted value is stored in the ponent. If the value conversion or value test fails, then an error message is generated and placed in the FacesContext, which displays the response phase, and the error message is displayed with any other test error. Conversion and inspection usually occur during the process of testing. Component to convert and store its submittedValue. For example, if the field is bound to a Integer property, then the value is converted to a Integer. If the value conversion fails, an error message is generated and placed in the FacesContext. The error message is displayed in the display response phase, along with any other test error.In the event of the first event of the life cycle after the application of the request value phase. At this stage, the value of each ponent is tested according to the test rules of the application. Inspection rules can be predefined (JSF attached), or can be defined by the developer. The value of user input is pared with the test rules. If the value of the input is not valid, an error is not valid.JSf生命周期恢復(fù)視圖,應(yīng)用請(qǐng)求值;處理事件,處理檢驗(yàn);處理事件,更新模型值;處理事件調(diào)用應(yīng)用程序;處理事件 表按照每個(gè)階段可能的執(zhí)行次序和事件處理進(jìn)行排列,但是 JSF 生命周期并不是固定的??梢愿淖儓?zhí)行的次序,跳過某些階段或完全脫離生命周期。打個(gè)比方,在對(duì)應(yīng)的組件上獲得一個(gè)無效的請(qǐng)求,當(dāng)前的視圖可以重新顯示,而且某些階段可以不顯示,同時(shí)可以不受jsf的控制,如果servlet接受另一個(gè)程序的請(qǐng)求,用戶可一重新將它重新定義它的資源管理,也就是轉(zhuǎn)發(fā)該請(qǐng)求到另一個(gè)web資源管理,當(dāng)這種情況發(fā)生的時(shí)候,可以執(zhí)行對(duì)應(yīng)的responseComplete方法,也就是無法將這個(gè)階段繼續(xù)執(zhí),行,用戶可以通過維護(hù)web的生命周期來控制程序的最終執(zhí)行結(jié)果,這種方法可以極大的簡(jiǎn)化了頁面開發(fā)的繁瑣。最重要的是,在利用生命周期組織您的開發(fā)工作的同時(shí)不會(huì)受其束縛。在需要時(shí)可以修改默認(rèn)的生命周期,而不必?fù)?dān)心破壞應(yīng)用程序。在執(zhí)行任何應(yīng)用程序邏輯之前,必須檢驗(yàn)表單;在執(zhí)行檢驗(yàn)之前,必須對(duì)字段數(shù)據(jù)進(jìn)行轉(zhuǎn)換。如果堅(jiān)持采用生命周期,您就可以集中精力考慮檢驗(yàn)和轉(zhuǎn)換的細(xì)節(jié),而不必關(guān)注請(qǐng)求過程本身的階段。還要注意,其他 Web 框架也有相似的生命周期;只不過沒這么明顯。 一些使用 JSF 的開發(fā)人員可能從來沒有編寫過組件或擴(kuò)展過框架,而其他開發(fā)人員的工作卻集中在這些任務(wù)上。 盡管對(duì)于幾乎任何項(xiàng)目, jsf相對(duì)于每個(gè)項(xiàng)目,它的生命周期都是差不多的,使用者可以依據(jù)自己在這個(gè)項(xiàng)目中具體的參與角色和項(xiàng)目進(jìn)行的不同階段。當(dāng)開發(fā)者是從事整體的應(yīng)用程序方面的開發(fā)。那么可以將生命周期分成幾個(gè)不同的階段。在 JSF 生命周期的第一個(gè)階段 — 恢復(fù)視圖 中,通過 FacesServlet servlet 發(fā)來一個(gè)請(qǐng)求。這個(gè) servlet 檢查這個(gè)請(qǐng)求并提取出視圖 ID(視圖 ID 由 JSP 頁面的名稱決定)。 JSf使用的是通過某個(gè)視圖的id找出某個(gè)manageBean的組件所在的位置,就是非常容易,如果這個(gè)視圖還不存在,JSF 控制器就創(chuàng)建它,然后調(diào)用get方法(返回一個(gè)null),然后調(diào)用set方法(此時(shí)后臺(tái)bing的組件將會(huì)被賦上值),然后進(jìn)入渲染階段。當(dāng)某個(gè)視圖已經(jīng)存在的時(shí)候,對(duì)應(yīng)的控件會(huì)按照一定的規(guī)則去調(diào)用它,此時(shí)框架內(nèi)的組件會(huì)將對(duì)應(yīng)的字段賦值,然后迅速的渲染該頁面。視圖包含所有 GUI 組件。生命周期的這個(gè)階段有三種視圖實(shí)例:新視圖、初始視圖和 postback,每種視圖的處理方法各不相同。對(duì)于新視圖,JSF 構(gòu)建一個(gè) Faces 頁面的視圖,并將事件處理函數(shù)和檢驗(yàn)器連接到組件。視圖保存在一個(gè) FacesContext 對(duì)象中。JacesContext 存儲(chǔ)狀態(tài)信息,JSF 需要使用這些信息為當(dāng)前請(qǐng)求管理 GUI 組件的狀態(tài)。FacesContext 將視圖存儲(chǔ)在它的 viewRoot 屬性中。相比之下在剛開始加載出的頁面,jsf需要?jiǎng)?chuàng)建一個(gè)空的視圖進(jìn)行渲染,加載頁面是,把查出來的值用來填充頁面,加載完成后,頁面會(huì)進(jìn)入顯示相應(yīng)的階段,用戶返回到某個(gè)界面時(shí),如果該頁面已經(jīng)存在,那么這個(gè)時(shí)候只需要回復(fù)之前的視圖就可以了,但是這種情況jsf需要重新構(gòu)造現(xiàn)有視圖的狀態(tài),然后獲取對(duì)應(yīng)的值,轉(zhuǎn)換和檢驗(yàn)一般發(fā)生在處理檢驗(yàn) 階段。組件轉(zhuǎn)換并存儲(chǔ)它的 submittedValue。例如,如果字段綁定到一個(gè) Integer 屬性,那么值就轉(zhuǎn)換為一個(gè) Integer。將用戶轉(zhuǎn)移到下一個(gè)頁面,對(duì)于成功的表單提交,可以定義特定的結(jié)果并返回這個(gè)結(jié)果。還要注意,其他 Web 框架也有相似的生命周期;只不過沒這么明顯。 一些使用 JSF 的 開發(fā)人員可能從來沒有編寫過組件或擴(kuò)展過框架,而其他開發(fā)人員的工作卻集中在這些任務(wù)上。 在這個(gè)階段,根據(jù)應(yīng)用程序的檢驗(yàn)規(guī)則檢驗(yàn)每個(gè)組件的值。檢驗(yàn)規(guī)則可以是預(yù)定義的(JSF 附帶的),也可以由開發(fā)人員定義。jsf需要?jiǎng)?chuàng)建一個(gè)空的視圖進(jìn)行渲染,加載頁面是,把查出來的值用來填充頁面,加載完成后,頁面會(huì)進(jìn)入顯示相應(yīng)的階段,用戶返回到某個(gè)界面時(shí),如果該頁面已經(jīng)存在,那么這個(gè)時(shí)候只需要回復(fù)之前的視圖就可以了,但是這種情況jsf需要重新構(gòu)造現(xiàn)有視圖的狀態(tài),然后獲取對(duì)應(yīng)的值。更新服務(wù)器端模型的實(shí)際值。只有綁定到一個(gè)組件的值的 bean 屬性被更新。注意,這個(gè)階段在檢驗(yàn)之后發(fā)生,所以可以確信復(fù)制到 bean 屬性的值是有效的(至少在表單字段級(jí)上有效;它們?cè)跇I(yè)務(wù)規(guī)則級(jí)上仍然可能是無效的)。 在生命周期的第五個(gè)階段 — 調(diào)用應(yīng)用程序 — JSF 控制器調(diào)用應(yīng)用程序來處理表單提交。jsf需要?jiǎng)?chuàng)建一個(gè)空的視圖進(jìn)行渲染,加載頁面是,把查出來的值用來填充頁面,加載完成后,頁面會(huì)進(jìn)入顯示相應(yīng)的階段,用戶返回到某個(gè)界面時(shí),如果該頁面已經(jīng)存在,那么這個(gè)時(shí)候只需要回復(fù)之前的視圖就可以了,但是這種情況jsf需要重新構(gòu)造現(xiàn)有視圖的狀態(tài),然后獲取對(duì)應(yīng)的值。JSF 獲得從動(dòng)作方法返回的對(duì)象并調(diào)用它的 toString() 方法。然后使用這個(gè)值作為導(dǎo)航規(guī)則的結(jié)果。(在 第 1 部分 中討論過導(dǎo)航規(guī)則的配置。) 在生命周期的第六個(gè)階段 — 顯示響應(yīng),顯示視圖和它的所有組件,這些組件都處于當(dāng)前狀態(tài)。如果對(duì)組件的屬性進(jìn)行的綁定,此時(shí)將會(huì)調(diào)用get方法。