按下命令按钮时,输入文本不与后端 bean 同步

发布于 2024-12-26 14:52:36 字数 419 浏览 1 评论 0原文

当我点击 commandButton 时,它会输入 Product.amount 开始时的值,而不是当前在输入框中键入的值。

<h:inputText value="#{product.amount}" />
<h:commandButton id="Button"
                 value="Buy"
                 tabindex="2" >
    <f:ajax listener="#{shopBean.addToCart(product.product, product.amount)}" 
            execute="@this" 
            render="@all" />            
</h:commandButton>

When I hit the commandButton it puts in the value that product.amount started as rather than what is currently typed in the input box.

<h:inputText value="#{product.amount}" />
<h:commandButton id="Button"
                 value="Buy"
                 tabindex="2" >
    <f:ajax listener="#{shopBean.addToCart(product.product, product.amount)}" 
            execute="@this" 
            render="@all" />            
</h:commandButton>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2025-01-02 14:52:36

您需要将输入字段包含在 execute 属性中,以便它也能得到处理,否则它将被完全忽略。

<h:inputText id="amount" ... />
<h:commandButton ...>
    <f:ajax execute="@this amount" ... />
</h:commandButton>

或者只是将整个内容放在一个表单中并使用 execute="@form"

<h:form>
    <h:inputText ... />
    <h:commandButton ...>
        <f:ajax execute="@form" ... />
    </h:commandButton>
</h:form>

顺便说一句,render="@all" 违背了使用 ajax 的主要优点之一。尝试仅渲染实际需要更新的组件。

You need to include the input field in the execute attribute of the <f:ajax> so that it get processed as well, otherwise it will simply be ignored altogether.

<h:inputText id="amount" ... />
<h:commandButton ...>
    <f:ajax execute="@this amount" ... />
</h:commandButton>

Or just put the whole in a single form and use execute="@form".

<h:form>
    <h:inputText ... />
    <h:commandButton ...>
        <f:ajax execute="@form" ... />
    </h:commandButton>
</h:form>

By the way, the render="@all" defeats one of the main advantages of using ajax. Try to render exactly only the component(s) which actually needs to be updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文