将参数传递给复合组件操作属性 [英] Pass Argument to a composite-component action attribute

查看:80
本文介绍了将参数传递给复合组件操作属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题确实说明了一切. 我尝试失败,并显示以下错误:

The title really says it all. I have made an attempt which failed with the error:

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>

什么是正确的方法?

推荐答案

这确实行不通.之后,您不能再像这样传递额外"参数.您所声明的method-signature必须在使用复合组件的一侧完成.例如

This is indeed not going to work. You cannot pass "extra" parameters afterwards like that. The method-signature as you have declared has to be fulfilled in the side where the composite component is been used. E.g.

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

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

The composite component implementation should just look like this

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


如果这不是您想要的,并且您真的想从复合组件的一面传递它,那么我可以想到两种传递额外参数的方法:使用 <f:setPropertyActionListner> 来让JSF将其设置为属性,然后再调用该操作.但是,两者都没有在复合组件中进行任何更改.您需要至少请求整个bean作为复合组件的属性.


If this is not what you want and you really want to pass it from the composite component side on, then I can think of two ways of passing extra arguments: using <f:attribute> with an action listener to pass it as an attidional component attribute, or <f:setPropertyActionListner> to let JSF set it as a property right before the action is invoked. But none of both are without changes in the composite component. You would need to request for at least the whole bean as an attribute of the composite component.

这是<f:setPropertyActionListener>的示例.这将在调用操作之前设置属性.

Here's an example with <f:setPropertyActionListener>. This sets property right before the action is been invoked.

<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应该看起来像

With the above example, the bean should look like

public class Bean {

    private String someString;

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

    // ...
}

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

If you adhere a specific convention, you can maybe even omit the property attribute altogether.

这篇关于将参数传递给复合组件操作属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆