通过 ui:include 将方法传递给自定义组件

pass method through ui:include to custom component

我有 3 个文件:

main.xhtml

<ui:include src="included.xhtml">
    <ui:param name="bean" value="#{invoices}" />
    <ui:param name="method" value="initExchange" />
</ui:include>

included.xhtml

<s:compositeComponent bean="#{bean}" beanMethod="#{method}"
                    buttonActionListener="#{bean[method]()}" />

compositeComponent.xhtml

<cc:interface>
    <cc:attribute name="bean" />
    <cc:attribute name="beanMethod" />
    <cc:attribute name="buttonActionListener" method-signature="void f()" />
</cc:interface>
<cc:implementation>
    <h:outputText value="#{cc.attrs.bean}" />
    <h:outputText value="#{cc.attrs.beanMethod}" />
    <p:commandButton actionListener="#{cc.attrs.buttonActionListener}" />

h:outputTexts 没问题。但是点击按钮会出现异常:

Target Unreachable, identifier 'bean' resolved to null

如何将方法传递给按钮的actionListener?

当复合组件中的 actionListener 像 included.xhtml:

中那样被写成 bean[method] 时有效
<p:commandButton actionListener="#{cc.attrs.bean[cc.attrs.beanMethod]}" />

因此,通常通过多个阶段传递方法属性应该始终由单独的 bean 和方法完成,而不是一个属性 'method-signature'。