gpt4 book ai didi

jsf - 试图理解立即 ="true"跳过不应该输入的输入

转载 作者:行者123 更新时间:2023-12-03 10:55:22 26 4
gpt4 key购买 nike

就在我以为我已经明白了的时候...... *叹气*

考虑以下 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:
public class TestBean {
private String didSomething = "Nothing done yet";
// + getter

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

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

从我读到的所有内容中,我会期待以下内容:
  • 当尝试在不为输入字段提供值的情况下执行 foo 时,该操作永远不会执行,因为在 processValidationsPhase 期间发生错误,导致页面在此阶段后直接重新呈现并显示错误消息。 didSomething的值保持不变。 (这按预期工作)
  • 当尝试在不为输入字段提供值的情况下执行 bar 时,该操作会在 applyRequestValuesPhase 期间执行因为直接属性。变量 didSomething被改变。 (这按预期工作)

  • 关于接下来会发生什么, this description状态:

    "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 之后。 . 然而,这不会发生。 相反,感觉网站仍然跳过所有剩余的阶段,输入字段没有被触摸。它重新渲染没有错误消息。

    有人可以向我解释我对它的工作原理的理解有什么问题吗?

    最佳答案

    immediate="true"在按钮上,确实在应用请求值阶段调用了该操作,并且跳过了所有其余阶段。这也是该属性的唯一要点:在应用请求值阶段立即处理(解码、验证、更新和调用)组件。
    所有没有 immediate="true" 的输入反正都被忽略了。只有具有 immediate="true" 的输入也会被处理,但这也会在应用请求值阶段发生。如果在应用请求值阶段已经发生了一切,为什么还要调用剩余的阶段?
    Debug JSF lifecycle文章您可以找到以下摘要,它应该启发何时(不)使用 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:

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

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

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


    也可以看看:
  • Why was "immediate" attribute added to the EditableValueHolders?
  • 关于jsf - 试图理解立即 ="true"跳过不应该输入的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12960718/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com