博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSF教程(9)——生命周期之Process Validations Phase
阅读量:6992 次
发布时间:2019-06-27

本文共 4341 字,大约阅读时间需要 14 分钟。

在这个过程其中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 
executeIds = pvc.getExecuteIds(); Collection
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。

哦,策略模式!

好像又不太像。这里不过传递不同的參数然后不同的操作而已。工厂?有些相似,可是不全然是,工厂最起码得有创建吧。想了一圈设计模式也不知道哪个能和这里的实现相相应,总之这里抽象了,复用性高了。耦合性恰到优点。

转载地址:http://oybvl.baihongyu.com/

你可能感兴趣的文章
页面加载条实现思路
查看>>
WEB高性能
查看>>
我也来拆个无线路由器,斐讯的FIR302M
查看>>
DevOps和容器:本地or云端,如何选择?
查看>>
ActiveMQ 初试
查看>>
linux-yum库建立和常见使用
查看>>
“两只小熊队”Alpha版本展示博客
查看>>
创建django的不同环境
查看>>
Top 10 command-line commands for managing Windows 7 desktops
查看>>
CentOS5.4安装samba服务
查看>>
学习笔记之简单工厂设计模式
查看>>
Spring+SpringMVC+MyBatis+Maven框架整合
查看>>
MFC读写文件
查看>>
linux优化
查看>>
手动制作mini linux详细步骤—之一
查看>>
kali密码离线破解
查看>>
Bootstrap优秀模板-Unify.2.6.2
查看>>
poj 3122 Pie (二分)
查看>>
在面试中如何展示虚拟机和内存调优技能
查看>>
C++命名空间学习笔记
查看>>