将Argument传递给复合组件操作属性

时间:2011-06-15 09:19:03

标签: jsf-2 facelets composite-component

标题真的说明了一切。 我已经尝试了失败的错误:

Illegal attempt to pass arguments to a composite component lookup expression (i.e. cc.attrs.[identifier]).

我的尝试看起来像这样:

<composite:interface>
  <composite:attribute name="removeFieldAction" method-signature="void action(java.lang.String)" />
</composite:interface>
<composite:implementation>
  <h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction('SomeString')}"/>
</composite:implementation>

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:32)

这确实无法奏效。之后你不能传递“额外”参数。您声明的method-signature必须在使用复合组件的一侧完成。 E.g。

<my:button action="#{bean.remove('Somestring')}" />

复合组件实现应该如下所示

<h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction}" />

如果这不是你想要的,你真的想从复合组件方面传递它,那么我可以想到两种传递额外参数的方法:使用<f:attribute>和动作监听器将它传递给一个attidional组件属性,或<f:setPropertyActionListner>让JSF在调用操作之前将其设置为属性。但是两者都没有在复合组件中没有变化。您至少需要请求整个bean作为复合组件的属性。

以下是<f:setPropertyActionListener>的示例。这会在调用操作之前设置属性。

<composite:interface>
    <composite:attribute name="bean" type="java.lang.Object" />
    <composite:attribute name="action" type="java.lang.String" />
    <composite:attribute name="property" type="java.lang.String" />
</composite:interface>
<composite:implementation>
    <h:commandButton value="Remove" action="#{cc.attrs.bean[cc.attrs.action]}">
        <f:setPropertyActionListener target="#{cc.attrs.bean[cc.attrs.property]}" value="Somestring" />
    </h:commandButton>
</composite:implementation>

将用作

<my:button bean="#{bean}" action="removeFieldAction" property="someString" />

使用上面的示例,bean应该看起来像

public class Bean {

    private String someString;

    public void removeFieldAction() {
        System.out.println(someString); // Somestring
        // ...
    }

    // ...
}

如果您遵守特定约定,您甚至可以完全省略property属性。

相关问题