简体   繁体   English

调试JSF生命周期 - 每个阶段到底发生了什么

[英]Debugging JSF Life Cycle - what exactly happens in each phase

I have decided to dig completely into JSF 2.0 as my project demands deep knowledge of it. 我已经决定完全深入研究JSF 2.0,因为我的项目需要深入了解它。 I am reading JSF Lifecyle Debug , a well written and awesome article on JSF Life cycle. 我正在阅读JSF Lifecyle Debug ,这是一篇关于JSF生命周期的精彩文章。 While reading this article, I have following confusions. 在阅读本文时,我有以下混淆。

  1. If it's an initial request, in Restore View Phase an empty View is created and straight Render Response Phase happens. 如果是初始请求,则在Restore View Phase ”中会创建一个空视图,并且会发生直接Render Response Phase There is no state to save at this point. 此时没有可以保存的州。 What actually happens in render response phase then? 然后在render response phase实际发生了什么? I am confused a little while I am running the example. 我在运行这个例子时感到很困惑。

  2. The article states that, retrieved input value is set in inputComponent.setSubmittedValue() in Apply Request Values phase. 文章指出,检索的输入值是在Apply Request Values阶段的inputComponent.setSubmittedValue()中设置的。 If validation and conversion passes, then the value gets set in inputComponent.setValue(value) and inputComponent.setSubmittedValue(null) runs. 如果验证和转换通过,则在inputComponent.setValue(value)设置inputComponent.setValue(value)并运行inputComponent.setSubmittedValue(null) On same point article states that, now if in the next post back request, value is changed, it is compared with the submitted value which would always be null on every post back, v alue change listener will be invoked. 在同一篇文章中指出,现在如果在下一个回发请求中,值被更改,则将其与提交的值进行比较,该值在每个回发时始终为空,将调用alue change listener It means if, we don't change the value even, as submittedValue would be null, valueChangeListener will always be invoked? 这意味着,如果,我们不更改值,因为submittedValue将为null,将始终调用valueChangeListener? I am confused on this statement. 我对此声明感到困惑。 Can someone elaborate on this? 有人可以详细说明吗?

  3. Article states the usage of immediate attribute. 文章陈述了immediate属性的使用。 If immediate attribute is set on an input component, than ideally Process Validation Phase is skipped, but all of the conversion and validation happens in Apply Request Values . 如果在输入组件上设置了immediate属性,则理想情况下会跳过Process Validation Phase ,但所有转换和验证都在Apply Request Values My point is, still when the conversion and validation is happening, what's the advantage of skipping the third phase? 我的观点是,仍然在转换和验证发生时,跳过第三阶段的优势是什么?

  4. What does the term retrieved value means? 该术语检索值的含义是什么?

  5. I would like to know, if lets say there are five fields on the view. 我想知道,如果让我们说视图上有五个字段。 Does JSF makes a list of some collection of these values and Apply Request Values and Process Validations phase iterate over them one by one? JSF是否列出了这些值的一些集合,并且Apply Request ValuesProcess Validations阶段逐个迭代它们?

  6. At the last point of this article where it states, when to use immediate attribute. 在本文的最后一点说明,何时使用immediate属性。 As per my understanding, if immediate attribute is set in both input component and command component, It will skip the phases from Apply Request Values to Invoke Application for any attribute not having immediate . 根据我的理解,如果在输入组件和命令组件中都设置了immediate属性,那么对于没有immediate数的任何属性,它将跳过从Apply Request Values到Invoke Application的阶段。 Then what does the last statement mean "Password forgotten" button in a login form with a required and immediate username field and a required but non-immediate password field. 那么最后一个语句在登录表单中是什么意思是“忘记密码”按钮,其中包含必需的即时用户名字段和必需但非直接的密码字段。

I know these are very basic confusions but clarity on these topics will definitely help sharpen the JSF knowledge. 我知道这些是非常基本的混淆,但这些主题的明确性肯定有助于提高JSF知识。

1: What actually happens in render response phase then? 1: 渲染响应阶段实际发生了什么?

Generating HTML output for the client, starting with UIViewRoot#encodeAll() . UIViewRoot#encodeAll()开始为客户端生成HTML输出。 You can see the result by rightclick, View Source in webbrowser (and thus NOT via rightclick, Inspect Element in webbrowser, as that will only show the HTML DOM tree which the webbrowser has built based on the raw HTML source code and all JavaScript events thereafter). 你可以通过右键单击webbrowser中的View Source来查看结果(因此不能通过右键单击webbrowser中的Inspect Element ,因为这只会显示webbrowser基于原始HTML源代码构建的HTML DOM树以及之后的所有JavaScript事件)。


2: it is compared with the submitted value which would always be null on every post back 2:将其与提交的值进行比较,该值在每个帖子上始终为空

Nope, it's being hold as an instance variable. 不,它被保存为实例变量。 JSF doesn't call getSubmittedValue() to compare it. JSF不会调用getSubmittedValue()来比较它。


3: My point is, still when the conversion and validation is happening, what's the advantage of skipping the third phase? 3: 我的观点是,当转换和验证发生时,跳过第三阶段的优势是什么?

This is answered in the bottom of the article, under Okay, when should I use the immediate attribute? 这可以在文章的底部回答, 好的,我什么时候应该使用immediate属性? . In a nutshell: prioritizing validation. 简而言之:优先验证。 If components with immediate="true" fail on conversion/validation, then components without immediate="true" won't be converted/validated. 如果具有immediate="true"组件在转换/验证时失败,则不会转换/验证没有immediate="true"组件。


4: What does the term retrieved value means? 4: 术语检索值意味着什么?

The "raw" value which the enduser has submitted (the exact input value which the enduser entered in the input form). 最终用户提交的“原始”值(最终用户在输入表单中输入的确切输入值)。 This is usually a String . 这通常是一个String If you're familiar with servlets, then it's easy to understand that it's exactly the value as you obtain by request.getParameter() . 如果您熟悉servlet,那么很容易理解它正是您通过request.getParameter()获得的值。


5: Does JSF makes a list of some collection of these values and Apply Request Values and Process Validations phase iterate over them one by one? 5: JSF是否列出了这些值的一些集合,并且Apply Request Values和Process Validations阶段逐个迭代它们?

Almost. 几乎。 The collection is already there in flavor of the JSF component tree. 该集合已经存在于JSF组件树的风格中。 JSF thus basically iterates over a tree structure, starting with FacesContext#getUIViewRoot() . 因此,JSF基本上遍历树结构,从FacesContext#getUIViewRoot()


6: Then what does the last statement mean "Password forgotten" button in a login form with a required and immediate username field and a required but non-immediate password field. 6: 那么最后一个语句是什么意思是登录表单中的“忘记密码”按钮,其中包含必需的即时用户名字段和必需但非直接的密码字段。

This way you can reuse the login form for the "password forgotten" case. 这样,您可以重复使用“忘记密码”案例的登录表单。 If you submit the "login" button, then obviously both the username and password fields must be validated. 如果您提交“登录”按钮,那么显然必须验证用户名和密码字段。 However if you submit the "password forgotten" button, then the password field shouldn't be validated. 但是,如果您提交“忘记密码”按钮,则不应验证密码字段。


That said, you may find the below JSF phases/lifecycle cheatsheet useful as well for a quick reference: 也就是说,您可以找到以下JSF阶段/生命周期备忘单,以便快速参考:

  • fc = FacesContext
  • vh = ViewHandler
  • in = UIInput
  • rq = HttpServletRequest
  • id = in.getClientId(fc);

1 RESTORE_VIEW 1 RESTORE_VIEW

 String viewId = rq.getServletPath(); fc.setViewRoot(vh.createView(fc, viewId)); 

2 APPLY_REQUEST_VALUES 2 APPLY_REQUEST_VALUES

 in.setSubmittedValue(rq.getParameter(id)); 

3 PROCESS_VALIDATIONS 3 PROCESS_VALIDATIONS

 Object value = in.getSubmittedValue(); try { value = in.getConvertedValue(fc, value); for (Validator v : in.getValidators()) v.validate(fc, in, value); } in.setSubmittedValue(null); in.setValue(value); } catch (ConverterException | ValidatorException e) { fc.addMessage(id, e.getFacesMessage()); fc.validationFailed(); // Skips phases 4+5. in.setValid(false); } 

4 UPDATE_MODEL_VALUES 4 UPDATE_MODEL_VALUES

 bean.setProperty(in.getValue()); 

5 INVOKE_APPLICATION 5 INVOKE_APPLICATION

 bean.submit(); 

6 RENDER_RESPONSE 6 RENDER_RESPONSE

 vh.renderView(fc, fc.getViewRoot()); 

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM