直接方法在复合组件中调用MethodExpression

时间:2018-07-24 15:48:07

标签: jsf java-ee primefaces mojarra

我目前在许多地方都有一组类似的素面组件:

<p:accordionPanel id="accordion"
                  value="#{backingBean.listValue}"
                  var="example"
                  multiple="true">
    <p:tab>
        <f:facet name="title">
            <h:outputText value="Title">
            <p:commandButton actionListener="backingBean.directDeleteMethod(example)"
                             value="Remove"
                             partialSubmit="true"
                             update="accordion"
                             process="@this accordion"
                             onclick="event.stopPropagation()">
        </f:facet>

        <!-- element in the tab go here -->
    </p:tab>
</p:accordionPanel>
<p:commandButton value="Create new"
                 actionListener="backingBean.directCreateMethod()"
                 partialSubmit="true"
                 update="accordion"
                 process="@this accordion" />

我正在尝试将其转换为可重用的复合组件,并且已经将其结构化为这样。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:composite="http://java.sun.com/jsf/composite"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <composite:interface>
        <composite:attribute name="value" required="true" />
        <composite:attribute name="var" required="false" default="item" />
        <composite:attribute name="tabTitle" required="true" />
        <composite:attribute name="allowMultipleOpen" required="false" default="false"/>
        <composite:attribute name="editMode" required="true" default="false"/>
        <composite:attribute name="deleteMethod" required="false" method-signature="void f(java.lang.Object)"/>
        <composite:attribute name="newLabel" required="false" default="New"/>
        <composite:attribute name="createMethod" required="false" method-signature="void f()"/>
    </composite:interface>

    <composite:implementation>
        <p:outputPanel id="#{cc.clientId}">
            <p:accordionPanel id="#{cc.id}_accordion"
                              value="#{cc.attrs.value}"
                              var="#{cc.attrs.var}"
                              multiple="#{cc.attrs.allowMultipleOpen}">
                <p:tab>
                    <f:facet name="title">
                        <h:outputText value="#{cc.attrs.tabTitle}" />
                        <c:if test="#{not empty cc.attrs.deleteMethod}">
                            <p:commandButton value="Remove"
                                             actionListener="#{cc.attres.deleteMethod}"
                                             partialSubmit="true"
                                             update="#{cc.id}_accordion"
                                             process="#{cc.id}_accordion"
                                             rendered="#{cc.attrs.editMode}"
                                             onclick="event.stopPropagation();"/>
                        </c:if>
                    </f:facet>

                    <composite:insertChildren />

                </p:tab>
            </p:accordionPanel>
            <c:if test="#{not empty cc.attrs.createMethod}">
                <p:commandButton value="#{cc.attrs.newLabel}"
                                 actionListener="#{cc.attrs.createMethod}"
                                 partialSubmit="true"
                                 update="#{cc.id}_accordion"
                                 process="#{cc.id}_accordion"
                                 rendered="#{cc.attrs.editMode}" />
            </c:if>
        </p:outputPanel>
    </composite:implementation>
</html>

当我这样使用它时:

<cmcustom:Accordion value="#{backingBean.listValue}"
                            tabTitle="Title"
                            editMode="#{backingBean.editMode}"
                            var="example"
                            createMethod="#{backingBean.createNewFormEvent()}"
                            deleteMethod="#{backingBean.deleteFormEvent(example)}">
        </cmcustom:Accordion>

jsf忽略create和delete方法表达式,并将它们视为值表达式,在页面加载时立即调用这些函数,而不必等到这些动作被调用。同样,当随后按下任意一个按钮时,将同时调用两个动作。传递给delete方法的值始终为null。有没有办法将参数化方法表达式传递给复合组件?

(在玻璃鱼4.1上使用mojarra 2.2.14)。

0 个答案:

没有答案