標籤:des c style class blog code
在這個過程當中JSF的實現者使用processValidators方法處理所有在tree中的組件中註冊的驗證器。驗證的過程就是通過每個組件已有的規則對其已經儲存的值進行校正,同時也對輸入的值進行校正,前提是組件的immediate屬性沒有設定為true。從代碼來看在UIViewRoot中的這個processValidators方法和上個階段中的processDecodes基本一致,不用說下一個階段(Update ModelValues Phase)也會有相類似的方法(processUpdates)。之所以JSF會這樣設計是因為這三個階段(取值、校正、更新)所作的事情對於每個組件(一個View的各個部分)而言是一致的。
PartialViewContextImpl中經典的一個方法,此方法將“處理”抽象了出來,妙不可言哇。
public void processPartial(PhaseId phaseId) { updateFacesContext(); PartialViewContext pvc = ctx.getPartialViewContext(); Collection <String> executeIds = pvc.getExecuteIds(); Collection <String> renderIds = pvc.getRenderIds(); UIViewRoot viewRoot = ctx.getViewRoot(); if (phaseId == PhaseId.APPLY_REQUEST_VALUES || phaseId == PhaseId.PROCESS_VALIDATIONS || phaseId == PhaseId.UPDATE_MODEL_VALUES) { // Skip this processing if "none" is specified in the render list, // or there were no execute phase client ids. if (executeIds == null || executeIds.isEmpty()) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "No execute and render identifiers specified. Skipping component processing."); } return; } try { processComponents(viewRoot, phaseId, executeIds, ctx); } catch (Exception e) { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, e.toString(), e); } } // If we have just finished APPLY_REQUEST_VALUES phase, install the // partial response writer. We want to make sure that any content // or errors generated in the other phases are written using the // partial response writer. // if (phaseId == PhaseId.APPLY_REQUEST_VALUES) { PartialResponseWriter writer = pvc.getPartialResponseWriter(); ctx.setResponseWriter(writer); } } else if (phaseId == PhaseId.RENDER_RESPONSE) { try { // // We re-enable response writing. // PartialResponseWriter writer = pvc.getPartialResponseWriter(); ResponseWriter orig = ctx.getResponseWriter(); ctx.getAttributes().put(ORIGINAL_WRITER, orig); ctx.setResponseWriter(writer); ExternalContext exContext = ctx.getExternalContext(); exContext.setResponseContentType("text/xml"); exContext.addResponseHeader("Cache-Control", "no-cache"); writer.startDocument(); if (isRenderAll()) { renderAll(ctx, viewRoot); renderState(ctx); writer.endDocument(); return; } // Skip this processing if "none" is specified in the render list, // or there were no render phase client ids. if (renderIds == null || renderIds.isEmpty()) { } else { processComponents(viewRoot, phaseId, renderIds, ctx); } renderState(ctx); writer.endDocument(); } catch (IOException ex) { this.cleanupAfterView(); } catch (RuntimeException ex) { this.cleanupAfterView(); // Throw the exception throw ex; } } }
如果本地值不合法,或者任何轉換失敗那麼JSF的實現將會在FacesContext執行個體中增加一條錯誤資訊,然後生命週期直接跳轉到Render Response階段頁面會被再次渲染並且附帶上剛才的錯誤資訊。當然之前產生的放在FacesContext中的錯誤資訊也會一併顯示出來。
如果在當前FacesContext執行個體中任何validate方法或者事件監聽器調用renderResponse方法JSF實現將跳到Render Response階段。
和上個階段一樣如果程式需要重新導向到不同web應用資源或者產生一個不包含JSF組件的響應那麼會調用FacesContext.responseComplete方法。
如果當前請求被定義為一個局部的請求,那麼局部內容會被從FacesContext中回複,而且局部處理方法會被執行。
細心的讀者可能看出來了後面的三種情況和上一步驟中的流程是驚人的相似,這歸根結底是因為JSF實現者人為的將Execute步驟分成了6個不同的階段,但是這六個階段中的中間三個( Apply Request Values Phase、Process Validations Phase、Update Model Values Phase)階段從本質上來看是一模一樣的。這三個階段都是為組件中值的展示服務的,不論是取值,還是校正,還是更新他們都是相同得邏輯不同的細節,這也就能解釋了為什麼三個階段可以公用上面貼出的一套代碼而只用傳入不同的PhaseId。哦,策略模式!好像又不太像,這裡僅僅是傳遞不同的參數然後不同的操作而已。工廠?有些類似,但是不完全是,工廠最起碼得有建立吧。想了一圈設計模式也不知道哪個能和這裡的實現相對應,總之這裡抽象了,複用性高了,耦合性恰到好處。