Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jsf - Pass method argument/parameter to composite-component action attribute

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...