JSF 按钮动作触发两次

JSF button action triggered twice

我已经在 WildFly 论坛上问过这个问题,但到目前为止还没有得到任何答案。所以我在这里尝试。

自从我从 WildFly 8.1 升级到 8.2 后,连接到 bean 的 tabView 中的命令按钮出现问题。

这是一个简单的 JSF 页面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head></h:head>
    <h:body>
        <h:form>
            <p:tabView binding="#{testBean.tabView}">
                <p:tab title="Tab">
                    <p:commandButton value="Inside" action="#{testBean.testInside}"/>
                </p:tab>
            </p:tabView>
            <p:commandButton value="Outside" action="#{testBean.testOutside}"/>
        </h:form>
    </h:body>
</html>

和豆子:

@Named
@SessionScoped
public class TestBean implements Serializable {
    private TabView tabView = new TabView();

    public TabView getTabView() {
        return tabView;
    }

    public void setTabView(TabView tabView) {
        this.tabView = tabView;
    }

    public void testInside() {
        System.out.println("inside");
    }

    public void testOutside() {
        System.out.println("outside");
    }
}

单击 "Inside" 按钮会触发 testInside() 两次 次。 "Outside" 按钮(在 tabView 之外)行为正常,仅触发一次方法。删除 tabView 绑定消除了这个问题。我使用 PrimeFaces 4.0。

感谢任何想法

一月

这是一个 Mojarra 'issue',由 2.2.7 中的性能优化修复引入。

this is a Mojarra "issue", I've discovered it when working on RF-13920, it was introduced by JAVASERVERFACES-3193. The components that use binding are not recreated during a request to the server but their children are. With the original children still in place inserting the new children causes the "duplicate id" error.

看起来你的按钮被添加了两次,但由于你没有分配明确的 ID,所以你不会收到重复 ID 错误...尝试一下可能会很有趣(添加明确的 ID )

The JSF specification states that binding should only be used in Request scope, so I don't think it should be treated as a bug if this fails in Conversation scope.

最后一句话很有趣。正如 post 在 jboss 站点上的下一个 post 中编辑的那样:

But now I think I have a satisfying solution for this issue and I can confirm, that with request-scoped backing beans for the component binding the exception and duplicate id problem does not occur anymore, even with Mojarra 2.2.8 from Wildfly-8.2.0.Final!

This is even true if the rest of the logic for the page remains in a (say) conversation scoped bean. You just need to have a request-scoped bean for the binding attribute, which then can be referenced in EL and other beans.

也检查this post

你可以试试其他方式。使用与 commandButton 相同的操作在选项卡外使用 remoteCommand。
然后在commandButton 的onclick 事件中使用remoteCommand 创建的JavaScript 函数。
这是使用您的代码的示例。
功能正常。

<h:form>
    <p:remoteCommand id="myfun" name="myfun" action="#{testBean.testInside}" />
    <p:tabView binding="#{testBean.tabView}">
        <p:tab title="Tab">
            <p:commandButton value="Inside" onclick="myfun();"/>
        </p:tab>
    </p:tabView>
</h:form>