是否可以在复合组件中调用参数化方法签名?

时间:2011-07-07 04:09:09

标签: jsf-2 composite-component

我目前正在创建一个带有可以接受参数的方法签名的JSF复合组件。

以下是摘录:

<composite:interface>
    ....
    <composite:attribute name="activateHeroMethod" method-signature="java.util.List action(id.co.sofcograha.core.Dto, id.co.sofcograha.core.Dto)" />
</composite:interface>

<composite:implementation>
    ....
    <p:commandLink value="#{hero.map['heroName']}"
        process="@this"
        update="#{cc.attrs.update}"
        oncomplete="infoRaceDialog.hide()"
        image="ui-icon ui-icon-search">
        <f:setPropertyActionListener value="#{hero}"
            target="#{cc.attrs.activateHeroMethod(infoRaceBean.race, hero)}" />
    </p:commandLink>
    ....
</composite:implementation>

target =“#{cc.attrs.activateHeroMethod(infoRaceBean.race,hero)}”严重失败,出现以下错误消息:

javax.faces.view.facelets.TagAttributeException: /resources/sofco/infoRace.xhtml @57,76 target="#{cc.attrs.activateHeroMethod(infoRaceBean.race, hero)}" /resources/sofco/infoRace.xhtml @57,76 target="#{cc.attrs.activateHeroMethod(infoRaceBean.race, hero)}" Illegal attempt to pass arguments to a composite component lookup expression (i.e. cc.attrs.[identifier]).
        at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:401)
        at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:351)
        at com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler.applyAttachedObject(SetPropertyActionListenerHandler.java:128)
        at com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler.apply(SetPropertyActionListenerHandler.java:101)

是否可以从复合组件的属性中调用参数化方法? 我认为我可以处理多个作为在一个方法中使用多个参数的解决方法,但我不喜欢这种方法,因为它的大小会随着参数的增长而增长。

我可以将每个参数包装成1个包装器对象,但我仍然很好奇是否有办法解决使用多个参数调用该方法的问题。

请分享您的想法!

谢谢。

3 个答案:

答案 0 :(得分:8)

是的可以做到这一点,这样做:

<composite:interface displayName="Image uploader with preview list">
        <composite:attribute name="backingBean" required="true" />
        <composite:attribute name="imageType" required="true"  />
</composite:interface>
....
<p:commandButton  value="Save"  actionListener="#{cc.attrs.backingBean.imageUploaded(cc.attrs.imageType)}"/>

希望这有助于某些人。

答案 1 :(得分:0)

复合组件:

<ui:component
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:cc="http://java.sun.com/jsf/composite"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:p="http://primefaces.org/ui"
>

    <cc:interface>

        <cc:attribute name="header" type="java.lang.String" required="true"/>

        <cc:attribute name="value" required="true"/>
        <cc:attribute name="update" type="java.lang.String" required="false" default=""/>
        <cc:attribute name="controller" required="true"/>

        <cc:attribute name="addLine" required="true" method-signature="void addLine()"/>
        <cc:attribute name="deleteLine" type="java.lang.String" required="true"/>

    </cc:interface>

    <cc:implementation>

        <div id="#{cc.clientId}">

            <p:panel header="#{cc.attrs.header}">

                <p:dataTable id="table" value="#{cc.attrs.value}" var="line">

                    <p:column headerText="#{cc.attrs.header}">
                        <h:outputText value="#{line.entry}"/>
                    </p:column>

                    <p:column width="32">
                        <f:facet name="header">
                            <p:commandButton title="#{msgs.add}" icon="fa fa-plus"
                                             type="button">
                                <f:ajax listener="#{cc.attrs.addLine}" render="#{cc.attrs.update} table"/>
                            </p:commandButton>
                        </f:facet>
                        <p:commandButton title="#{msgs.delete}" icon="fa fa-minus"
                                         type="button">
                            <f:ajax listener="#{cc.attrs.controller[cc.attrs.deleteLine](line)}" render="#{cc.attrs.update} table"/>
                            <p:confirm message="#{msgs.deleteString}" icon="ui-icon-alert"/>
                        </p:commandButton>
                    </p:column>

                </p:dataTable>

            </p:panel>

        </div>

    </cc:implementation>

</ui:component>

Page:

<ui:composition
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:any="http://java.sun.com/jsf/composite/components"
>

    <!--@elvariable id="controller" type="org.controllers.AnyController"-->
    <any:row
            header="#{msgs.subjective}"
            value="#{controller.entity.things}"
            update="form:growl form:save"
            controller="#{anyController}"
            addLine="#{anyController.addTheLine}"
            deleteLine="deleteTheLine"/>

</ui:composition>

Controller:

@javax.inject.Named
@javax.enterprise.context.SessionScoped
public class AnyController
{
    ...

    public void addTheLine()
    {
        ...
    }

    public void deleteTheLine(final Line anyLine)
    {
        ...
    }

    ...
}

答案 2 :(得分:0)

任何遇到同样问题的人都可以尝试这样的事情

<composite:attribute name="methodName" method-signature="java.lang.String f(java.lang.String, java.lang.String)" required="false" />