JSF2.0 - 具有可选方法表达式的复合组件

Eri*_*ves 7 composite-component java-ee-6 jsf-2

我正在实现一个复合组件,我发现了一个我找不到解决方案的问题.

我指定了页面作者可以传递或不传递的属性,但我无法指定方法属性(Action的方法表达式),如果未传递,则复合组件不使用method属性在composite:implementation标签中.

这是我的代码:

<composite:interface>
    <composite:attribute name="namePrompt" required="true"/>
    <composite:attribute name="actionMethod" method-signature="java.lang.String  action()" required="false"/>
    <composite:attribute name="showComponent" default="false"/>
</composite:interface>

<composite:implementation>
    <div>
       <p:commandLink actionListener="#{cc.attrs.actionMethod}"
                      rendered="#{cc.attrs.showComponent}"
                      >
            <h:outputText value="#{cc.attrs.namePrompt}"/>    
       </p:commandLink>
    </div>
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

使用它时,我没有指定"actionMethod"属性.像这样:

<util:foo namePrompt="SomeName" showComponent="true"/>
Run Code Online (Sandbox Code Playgroud)

但我收到错误消息:

javax.faces.FacesException: Unable to resolve composite component from using page using EL expression '#{cc.attrs.actionMethod}'
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

par*_*lov 10

您将必须创建两个p:commandLink元素并根据参数的定义有条件地渲染它们:

<p:commandLink actionListener="#{cc.attrs.actionMethod}" rendered="#{!empty cc.getValueExpression('actionMethod') and cc.attrs.showComponent}">
  <h:outputText value="#{cc.attrs.namePrompt}"/>
</p:commandLink>
<p:commandLink rendered="#{empty cc.getValueExpression('actionMethod')}">
  <h:outputText value="#{cc.attrs.namePrompt}"/>
</p:commandLink>
Run Code Online (Sandbox Code Playgroud)


Tho*_*gor 5

将方法签名返回类型更改为java.lang.Object并添加"null"作为默认值.

<composite:interface>
    <composite:attribute name="namePrompt" required="true"/>
    <composite:attribute name="actionMethod" method-signature="java.lang.Object action()" required="false" default="null"/>
    <composite:attribute name="showComponent" default="false"/>
</composite:interface>

<composite:implementation>
    <div>
       <p:commandLink actionListener="#{cc.attrs.actionMethod}"
                      rendered="#{cc.attrs.showComponent}"
                      >
            <h:outputText value="#{cc.attrs.namePrompt}"/>    
       </p:commandLink>
    </div>
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

没有方法:

<util:foo namePrompt="SomeName" showComponent="true"/>
Run Code Online (Sandbox Code Playgroud)

方法:

<util:foo actionMethod="#{someBean.someMethod()}" namePrompt="SomeName" showComponent="true"/>
Run Code Online (Sandbox Code Playgroud)