【问题标题】:Pass method argument/parameter to composite-component action attribute将方法参数/参数传递给复合组件操作属性
【发布时间】:2011-09-15 09:07:57
【问题描述】:

标题确实说明了一切。 我进行了一次尝试,但由于错误而失败:

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>

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

【问题讨论】:

    标签: jsf facelets composite-component


    【解决方案1】:

    这确实行不通。之后你不能像那样传递“额外”参数。您声明的method-signature 必须在使用复合组件的一侧实现。例如

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

    复合组件实现应该是这样的

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

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

    以下是&lt;f:setPropertyActionListener&gt; 的示例。这会在调用操作之前设置属性。

    <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 属性。

    【讨论】:

    • Tnx。想法是复合组件创建一个字段列表,每个字段都链接到 bean 中的一个对象。从 UI 中删除字段后,必须通知 bean 已删除字段的 ID,以便它也将从 bean 中删除。因此,someString 参数实际上是已删除字段的 UUID。在实践中,我正在尝试实现类似于带有参数的事件侦听器...感谢您的解决方案!
    • 嗨 BalusC。非常感谢您的回复。这适用于 Mojarra,但似乎不适用于 MyFaces。我在这里发布了一个单独的问题,请您有空时看看吗? stackoverflow.com/questions/17357593/…非常感谢
    • 这仍然相关吗?这很麻烦,应该有更好的方法从复合组件返回值@BalusC
    猜你喜欢
    • 2018-08-03
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多