JSF h:commandButton是否可以触发表单内的其他命令按钮?

3

我有一个表单,其中包含几个h:commandButtons-所有按钮都附有f:ajax标签。当我触发其中一个命令按钮时,我在日志中看到所有实际上都被触发了-所以所有操作都被调用了?

这是h:commandButton + f:ajax组合的正常行为吗?

我使用commandButton的action属性而不是f:fajax上的listener。

希望有人能向我解释一下。

提前致谢。

/ Søren

编辑:

简化的代码:

<h:form>
  <ui:repeat ...>
     <h:inputText value="#{order.quantity}"/>
  </ui:repeat>
  <h:commandButton type="button" action="#{facade.updateOrderItems()}" value="Update">
     <f:ajax execute="@form" render=":orderList" onevent="onAjaxEvent"></f:ajax>
  </h:commandButton>
  <h:commandButton type="button" action="#{facade.deleteSelectedOrderItems(order)}" value="Delete">
    <f:ajax execute="@form" render=":orderList" onevent="onAjaxEvent"></f:ajax>
  </h:commandButton>
  <h:commandButton type="button" action="#{facade.addOrdersItemsToWishList(order)}" value="Add to wish list"> <f:ajax execute="@form" render=":orderList" onevent="onAjaxEvent"></f:ajax>
                                </h:commandButton>
</h:form>

当我点击更新按钮时,似乎也会调用删除和添加到愿望清单按钮...?

很奇怪。请发布一个代码示例以展示这个错误。 - Luiggi Mendoza
尝试在您的<h:commandButton>中使用immediate=true,像这样: <h:commandButton immediate=true action=..../> - Syed Anas
immediate=true 只解决了部分问题。其他按钮没有执行 - 但是我的 inputText 值没有设置在 bean 中 :-( 如果我用 @ form 替换 @ this,似乎一切都正常工作,但是我仍然无法将数据传递到我的 bean 中... - sorenchristensen
1个回答

3

我希望还不算太晚。两天前我也遇到了同样的问题,但现在终于解决了。问题的原因是type="button"。所以,解决方法就是去掉这个属性。在你的f:ajax中,你需要使用execute="@form"来触发input字段的valueChange事件,以便将数据保存到bean中,否则只会执行command-button而不会注册valueChange事件。所以你的按钮应该像这样:

<h:commandButton action="#{facade.deleteSelectedOrderItems(order)}" value="Delete">
   <f:ajax execute="@form" render=":orderList" onevent="onAjaxEvent"></f:ajax>
</h:commandButton>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接