b:commandButton 在标记文件中使用动作调用方法两次 侦听器被添加到动作

b:commandButton in tagfile invokes methods twice with action listener is added to action

我试着写一个jsf标签库。所以我创建了一个提供 facelet 的库: AbButton.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:b="http://bootsfaces.net/ui" xmlns:o="http://omnifaces.org/ui"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <c:choose>
        [... other button variants...]
        <c:when test="#{type == 'command' and not empty look}">
            <b:commandButton value="#{value}" action="#{bean[action]}" id="#{id}" actionListener="#{bean[actionListener]}"
                         update="#{update}" look="#{look}" size="#{size}" iconAwesome="#{icon}" style="#{style}"/>
        </c:when>
    </c:choose>
</ui:composition>

并且我创建了一个描述属性的 taglib.xml。

<tag>
    <tag-name>abButton</tag-name>
    <description><![CDATA[Flexible Button Implementierung. Von Bootsfaces Button abgeleitet.]]></description>
    <source>resources/tags/AbButton.xhtml</source>

    [...]
    <attribute>
        <description><![CDATA[ stuff]]></description>
        <name>action</name>
        <required>false</required>
        <type>javax.el.MethodExpression</type>
    </attribute>
    <attribute>
        <description><![CDATA[stuff]]></description>
        <name>actionListener</name>
        <required>false</required>
        <type>javax.el.MethodExpression</type>
    </attribute>
    [...]
</tag>

最后但同样重要的是,我在测试应用程序中实现了它。 Button.xhtml

<h:body>
    <h:form>
        <a:abButton type="command" id="command" bean="#{button}" action="action" actionListener="actionListener" value="Command Button" style="margin-right:20px;" look="primary"/>
    </h:form>
</h:body>

还有一个名为 Button.java

的 Backing Bean
@Named("button")
@RequestScoped
public class Button {

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

    public void actionListener(){
        System.out.println("action listener");
    }
}

如果我现在点击按钮,action 和 actionListener 方法都会执行 2 次。如果我删除 actionListener,则 action 方法只会执行一次。同样的事情正在发生,我使用不同的豆子,比方说

AbButton.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:b="http://bootsfaces.net/ui" xmlns:o="http://omnifaces.org/ui"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <c:choose>
        [... other button variants...]
        <c:when test="#{type == 'command' and not empty look}">
            <b:commandButton value="#{value}" action="#{bean2[action]}" id="#{id}" actionListener="#{bean[actionListener]}"
                         update="#{update}" look="#{look}" size="#{size}" iconAwesome="#{icon}" style="#{style}"/>
        </c:when>
    </c:choose>
</ui:composition>

和Button.xhtml

<h:body>
    <h:form>
        <a:abButton type="command" id="command" bean="#{button}" bean2="#{button}" action="action" actionListener="actionListener" value="Command Button" style="margin-right:20px;" look="primary"/>
    </h:form>
</h:body>

如果我这样使用 Omnifaces o:methodParam,绝对会发生同样的情况:

AbButton.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:b="http://bootsfaces.net/ui" xmlns:o="http://omnifaces.org/ui"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <c:choose>
        [... other button variants...]
        <c:when test="#{type == 'command' and not empty look}">
            <o:methodParam name="actionMethod" value="#{action}"/>
            <o:methodParam name="actionListenerMethod" value="#{actionListener}"/>
            <b:commandButton value="#{value}" action="#{actionMethod}" actionListener="#{actionListenerMethod}" id="#{id}" look="#{look}"
                         update="#{update}" size="#{size}" iconAwesome="#{icon}" style="#{style}"/>
        </c:when>
    </c:choose>
</ui:composition>

和button.xhtml

<h:body>
    <h:form>
        <a:abButton type="command" id="command" action="#{button.action}" actionListener="#{button.actionListener}" value="Command Button" style="margin-right:20px;" look="primary"/>
    </h:form>
</h:body>

同样的情况。在 action 和 actionListener 处于活动状态时,两者都会执行两次。如果我删除 actionListener,则 action 方法只执行一次。我希望现在可以更好地理解它。有帮助吗?

啊,我明白了。这是与此处相关的 bootsfaces 0.8.1 中的错误 Issue 295。随着对 B​​ootsfaces 0.8.5 的更新,一切都按预期工作。也许这可以帮助面临同样问题的人。