Pass Argument to a composite-component action attribute

点点圈 提交于 2019-12-17 08:53:36

问题


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]).

My attempt looks like this:

<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>

What's the right way to do this?


回答1:


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}" />

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.

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>

which is to be used as

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

With the above example, the bean should look like

public class Bean {

    private String someString;

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

    // ...
}

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



来源:https://stackoverflow.com/questions/6355543/pass-argument-to-a-composite-component-action-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!