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:
<comp开发者_运维技巧osite: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?
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.
精彩评论