且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么是“立即"?添加到 EditableValueHolders 的属性?

更新时间:2022-05-24 00:56:35

用于对同一表单中的多个 EditableValueHolder 组件进行优先级验证.

It's to be used to prioritize validation on several EditableValueHolder components in the same form.

想象一个表单,它包含带有 immediate="true" 的输入组件和没有这个属性的输入组件.立即输入将在应用请求值阶段(比平时早一个阶段)进行验证.非即时输入将在验证阶段(这是通常的阶段)进行验证.如果至少一个即时输入的验证失败,则非即时输入根本不会被转换/验证,因此不会生成任何转换/验证错误消息.这在具有复杂验证规则的表单中特别有用,当(立即)组件 X 的验证无论如何失败时,验证组件 Y 没有意义.

Imagine a form containing input components with immediate="true" as well as input components without this attribute. The immediate inputs will be validated during apply request values phase (which is one phase earlier than usual). The non-immediate inputs will be validated during validations phase (which is the usual phase). If validation fails for at least one of the immediate inputs, then the non-immediate inputs won't be converted/validated at all and thus won't generate any conversion/validation error messages. This is particularly useful in forms with complex validation rules where it doesn't make sense to validate component Y when validation for (immediate) component X has failed anyway.

当在相同形式的命令按钮上与 immediate="true" 结合使用时,这将导致所有非立即输入被完全跳过.一个很好的现实世界示例是具有 2 个字段用户名"的登录表单.和密码"使用 required="true" 和 2 个按钮:登录"和忘记密码".您可以将 immediate="true" 放在用户名"上字段和忘记密码"按钮跳过密码字段上的 required="true" 检查.

When used in combination with immediate="true" on a command button in the same form, this will cause all non-immediate inputs being completely skipped. A good real world example is a login form with 2 fields "username" and "password" with required="true" and 2 buttons: "login" and "password forgotten". You could put immediate="true" on the "username" field and the "password forgotten" button to skip the required="true" check on the password field.

在黑暗的 JSF 1.x 时代,immediate="true" 也经常 (ab) 与 valueChangeListener 结合用作黑客>FacesContext#renderResponse(),在级联下拉列表中经常出现.长话短说,这是一篇关于此的旧博客文章.就此而言,它使开发人员能够在 更改时执行支持 bean 方法,而无需验证同一表单中的所有其他输入.但是现在,随着 ajax 的强大,这个 hack 是不必要的.您可以在我们的 wiki 页面的底部找到此案例的具体示例.

In the dark JSF 1.x ages, the immediate="true" was also often (ab)used as a hack in combination with valueChangeListener and FacesContext#renderResponse(), more than often in cascading dropdown lists. Long story short, here's an old blog article on that. To the point, it enables developers to execute a backing bean method on change of a <h:selectOneMenu> without that all other inputs in the same form are been validated. But these days, with the ajax awesomeness, this hack is unnecessary. You can find a concretre example of this case at the bottom of our <h:selectOneMenu> wiki page.

如今,immediate="true" 仍然经常 (ab) 使用,以便拥有一个完全绕过所有其他输入的特定按钮,例如;"中的注销按钮.神形"反模式(其中 一切 都被放在一个巨大的

中),或者一个错误地提交表单的取消按钮.当您开始真正需要 immediate="true" 以正确方式输入其中一个时,这样的按钮就会中断.您***将这样的注销按钮放在自己的表单中,或者将其更改为仅处理自己(PrimeFaces 中的 process=@this").而且你***把这样的取消按钮改成只是通过 <h:button value=Cancel" 来同步刷新页面./>.如果表单绑定到请求/视图范围的 bean 并且浏览器缓存是 在动态页面上禁用.

These days, the immediate="true" is still often (ab)used in order to have a specific button which completely bypasses all other inputs, such as a logout button in a "God-form" antipattern (whereby everything is been thrown together in a huge <h:form>), or a cancel button which incorrectly submits the form. Such a button would break when you start to actually need the immediate="true" the right way on one of the inputs. You'd better put such a logout button in its own form, or to change it to process only itself (process="@this" in PrimeFaces). And you'd better change such a cancel button to just refresh the page synchronously by <h:button value="Cancel" />. This works fine if the form is tied to a request/view scoped bean and browser caching is disabled on dynamic pages.