b:tagfile中的commandButton调用方法两次,并将动作侦听器添加到action中

时间:2016-05-18 15:07:12

标签: jsf commandbutton bootsfaces

我尝试编写一个jsf标签库。所以我创建了一个提供facelet的libary: 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方法。我希望现在更好理解。有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

啊,现在我知道了。这是与此处相关的bootsfaces 0.8.1中的一个错误Issue 295。随着Bootsfaces 0.8.5的更新,一切都按预期工作。也许这可以帮助遇到同样问题的人。