首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >B: tagfile中的命令action与动作侦听器一起调用两次方法被添加到操作中

B: tagfile中的命令action与动作侦听器一起调用两次方法被添加到操作中
EN

Stack Overflow用户
提问于 2016-05-18 15:07:11
回答 1查看 246关注 0票数 0

我试着写一个jsf标签libary。因此,我创建了一个libary,它提供了一个facelet: AbButton.xhtml

代码语言:javascript
复制
<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。

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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的支持豆

代码语言:javascript
复制
@Named("button")
@RequestScoped
public class Button {

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

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

如果我现在使用这个按钮,动作和actionListener方法都会被执行两次。如果删除actionListener,则操作方法只执行一次。同样的事情正在发生,我使用不同的bean,让我们说

AbButton.xhtml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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 :methodParam这样做,也会发生同样的情况:

AbButton.xhtml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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 active,将执行两次。如果删除actionListener,则操作方法只执行一次。我希望这是一个更好的理解。有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-19 09:12:26

啊,现在我明白了。这是与第295期相关的bootsfaces 0.8.1中的一个bug。通过对Bootsfaces 0.8.5的更新,一切都如预期的那样工作。也许这能帮助那些面临同样问题的人。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37303567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档