试图理解即时=“真".不应该跳过输入 [英] Trying to understand immediate="true" skipping inputs when it shouldn't

查看:109
本文介绍了试图理解即时=“真".不应该跳过输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在我以为我能立即理解... *叹*

Just when I thought I had understood immediate... *sigh*

考虑以下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}" />

还有这个支持bean:

And this backing 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:

  • 当尝试在不为输入字段提供值的情况下执行foo时,将永远不会执行该操作,因为在processValidationsPhase期间会发生错误,从而导致在此阶段之后直接重新呈现页面并显示一个错误信息. didSomething的值保持不变. (这按预期工作)

  • 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. The value of the didSomething remains unchanged. (This works as expected)

当尝试在不为输入字段提供值的情况下进行bar操作时,由于具有即时属性,因此在applyRequestValuesPhase期间执行了该操作.变量didSomething已更改. (这按预期工作)

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. The variable didSomething is changed. (This works as expected)

接下来会发生什么,此描述指出:

On what happens next, this description states:

空返回值(作为action方法的结果)导致处理继续正常进行,即验证了非立即组件,然后执行update-model(如果未发生验证错误).对于action侦听器方法,返回void,则如果不需要正常流程,则必须调用facesContext.renderResponse();"

"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."

据此,我的想法是处理正常进行(因为我的操作方法既不返回结果也不强制renderResponse()),导致相同的验证错误.唯一的不同是,它会在设置didSomething 之后发生. 但是,这不会发生..相反,感觉该站点仍在跳过所有剩余阶段,而不会触摸输入字段.它会重新呈现,而不会显示错误消息.

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. Only difference would be that it occurs after setting 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.

有人可以向我解释我对这项工作原理的理解不正确吗?

推荐答案

在按钮上按下immediate="true"时,确实在应用请求值阶段期间调用了该动作,并且跳过了所有其余阶段.这也是此属性的唯一要点:在应用请求值阶段立即处理(解码,验证,更新和调用)组件.

With immediate="true" on the button, the action is indeed invoked during apply request values phase and all the remaining phases are skipped. That's also the sole point of this attribute: process (decode, validate, update and invoke) the component immediately during apply request values phase.

所有没有具有immediate="true"的输入都将被忽略.仅处理确实具有immediate="true"的输入,但是在应用请求值阶段也会发生这种情况.如果在申请请求值"阶段中已经发生所有事情,为什么还要调用其余阶段?

All inputs which do not have immediate="true" are ignored anyway. Only inputs which do have immediate="true" are also processed, but this happens also during apply request values phase. Why should the remaining phases be invoked if everything has already taken place in the apply request values phase?

调试JSF生命周期文章中,您可以找到以下摘要应启发何时(不)使用immediate"true":

In the Debug JSF lifecycle article you can find the following summary which should enlighten when to (not) use the immediate"true":

好吧,我什么时候应该使用即时属性?

如果还不清楚,这里是一个总结,并附有一些可能有用的现实使用示例:

Okay, when should I use the immediate attribute?

If it isn't entirely clear yet, here's a summary, complete with real world use examples when they may be beneficial:

  • 如果仅在UIInput中进行设置,则将在应用请求值"阶段中进行过程验证阶段.使用它可以优先考虑所讨论的UIInput组件的验证.当其中任何一个的验证/转换失败时,将不对非立即组件进行验证/转换.

  • If set in UIInput(s) only, the process validations phase will be taken place in apply request values phase instead. Use this to prioritize validation for the UIInput component(s) in question. When validation/conversion fails for any of them, the non-immediate components won't be validated/converted.

如果仅在UICommand中设置,则对于任何UIInput组件,都会跳过应用请求值阶段,直到具有更新模型值阶段为止.使用它可以跳过表单的整个处理过程.例如. 取消"或返回"按钮.

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). Use this to skip the entire processing of the form. E.g. "Cancel" or "Back" button.

如果同时在UIInputUICommand组件中设置,则对于任何不具有此属性的UIInput组件,将跳过应用请求值阶段,直到具有更新模型值阶段为止放.使用此选项可跳过对某些字段(立即数)的整个表单期望的处理.例如.登录表单中的忘记了密码"按钮,其中包含必填但非立即的密码字段.

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. Use this to skip the processing of the entire form expect for certain fields (with immediate). E.g. "Password forgotten" button in a login form with a required but non-immediate password field.

另请参见:

  • 为什么立即"属性添加到EditableValueHolders吗?
  • See also:

    • Why was "immediate" attribute added to the EditableValueHolders?
    • 这篇关于试图理解即时=“真".不应该跳过输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆