简体   繁体   English

尝试理解立即=“真实”跳过输入,而不应该

[英]Trying to understand immediate=“true” skipping inputs when it shouldn't

Just when I thought I had understood immediate... *sigh* 就在我以为我立即理解的时候... *叹气*

Consider the following JSF page: 考虑以下JSF页面:

<h:inputText value="#{testBean.text}" required="true" />
<h:commandButton actionListener="#{testBean.doFoo}" value="Do Foo" />
<h:commandButton immediate="true" actionListener="#{testBean.doBar}" value="Do Bar" /><br />
<h:outputText value="#{testBean.didSomething}" />

And this backing bean: 而这个支持bean:

public class TestBean {
   private String didSomething = "Nothing done yet";
   // + getter

public void doFoo() {
    didSomething = "Did foo!";        
}

public void doBar() {
    didSomething = "Did bar!";        
}

From all I read about immediate I would expect the following: 从我所读到的所有内容中我可以期待以下内容:

  • When trying to do foo while not providing a value for the input field, the action is never executed because during processValidationsPhase an error occurs, resulting in the page to be re-rendered directly after this phase with an error message. 在没有为输入字段提供值的情况下尝试执行foo时,操作永远不会执行,因为在processValidationsPhase期间发生错误,导致在此阶段之后直接重新呈现页面并显示错误消息。 The value of the didSomething remains unchanged. didSomething的值保持不变。 (This works as expected) (这按预期工作)

  • When trying to do bar while not providing a value for the input field, the action is executed during applyRequestValuesPhase because of the immediate attribute. 当在没有为输入字段提供值的情况下尝试执行bar时,由于immediate属性,在applyRequestValuesPhase期间执行该操作。 The variable didSomething is changed. 变量didSomething已更改。 (This works as expected) (这按预期工作)

On what happens next, this description states: 接下来会发生什么, 这个描述说明:

"A null return value (as outcome of the action method) causes processing to continue as normal, ie non-immediate components are validated then update-model is executed (if no validation errors occurred). For an action listener method that returns void, it is necessary to call facesContext.renderResponse(); if the normal flow is not desired." “null返回值(作为action方法的结果)导致处理继续正常进行,即验证非立即组件然后执行update-model(如果没有发生验证错误)。对于返回void的动作侦听器方法,有必要调用facesContext.renderResponse();如果不需要正常流程。“

From this I had the idea that processing continues as normal (as my action method does neither return an outcome nor force renderResponse() ), resulting in the same validation error. 从此我开始认为处理正常进行(因为我的动作方法既不返回结果也不强制renderResponse() ),导致相同的验证错误。 Only difference would be that it occurs after setting didSomething . 唯一的区别是它设置didSomething 之后发生。 However, this does not happen. 但是,这不会发生。 Instead, it feels like the site still skips all remaining phases, with the input field not being touched. 相反,感觉网站仍然会跳过所有剩余的阶段,输入字段未被触及。 It re-renders without error message. 它重新呈现没有错误消息。

Can someone explain to me where my understanding of how this works is amiss? 有人可以向我解释我对这是如何工作的理解是不对的吗?

With immediate="true" on the button, the action is indeed invoked during apply request values phase and all the remaining phases are skipped. 如果按钮上的immediate="true" ,则在应用请求值阶段确实会调用该操作,并且会跳过所有剩余的阶段。 That's also the sole point of this attribute: process (decode, validate, update and invoke) the component immediately during apply request values phase. 这也是此属性的唯一要点:在应用请求值阶段期间立即处理(解码,验证,更新和调用)组件。

All inputs which do not have immediate="true" are ignored anyway. 无论如何,忽略所有没有 immediate="true"输入。 Only inputs which do have immediate="true" are also processed, but this happens also during apply request values phase. 仅处理具有immediate="true"输入,但这也在应用请求值阶段期间发生。 Why should the remaining phases be invoked if everything has already taken place in the apply request values phase? 如果在申请请求值阶段已经发生了所有事情,为什么还要调用其余阶段?

In the Debug JSF lifecycle article you can find the following summary which should enlighten when to (not) use the immediate"true" : Debug JSF生命周期文章中,您可以找到以下摘要,该摘要应该启用何时(不)使用immediate"true"

Okay, when should I use the immediate attribute? 好的,什么时候应该使用immediate属性?

If it isn't entirely clear yet, here's a summary, complete with real world use examples when they may be beneficial: 如果还不完全清楚,这里有一个摘要,当它们可能有益时,可以使用真实世界的使用示例:

  • If set in UIInput (s) only, the process validations phase will be taken place in apply request values phase instead. 如果仅在UIInput设置,则过程验证阶段将在应用请求值阶段中进行。 Use this to prioritize validation for the UIInput component(s) in question. 使用此选项来确定相关UIInput组件的验证优先级。 When validation/conversion fails for any of them, the non-immediate components won't be validated/converted. 当任何一个验证/转换失败时,将不验证/转换非直接组件。

  • If set in UICommand only, the apply request values phase until with update model values phases will be skipped for any of the UIInput component(s). 如果仅在UICommand设置,则应用请求值阶段直到更新模型值将跳过任何UIInput组件的阶段。 Use this to skip the entire processing of the form. 使用此选项可跳过表单的整个处理过程。 Eg "Cancel" or "Back" button. 例如“取消”或“后退”按钮。

  • If set in both UIInput and UICommand components, the apply request values phase until with update model values phases will be skipped for any of the UIInput component(s) which does not have this attribute set. 如果在UIInputUICommand组件中都设置了应用请求值阶段,那么对于没有设置此属性的任何UIInput组件,将跳过更新模型值阶段。 Use this to skip the processing of the entire form expect for certain fields (with immediate). 使用此选项可以跳过对某些字段(即时)的整个表单的处理。 Eg "Password forgotten" button in a login form with a required but non-immediate password field. 例如,登录表单中的“忘记密码”按钮,其中包含必需但非直接的密码字段。

See also: 也可以看看:

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

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