How to pass a action string into a JSF 2 composite component?

Brian Leathem picture Brian Leathem · May 17, 2010 · Viewed 13.9k times · Source

I'm creating a simple menuing composite component in JSF 2. However, I am unable to pass a String attribute into the composite component to use in the action attribute of the <h:commandLink>. My component looks like:

<composite:interface>
    <composite:attribute name="title" required="true" type="java.lang.String"/>
    <composite:attribute name="view" required="true" />
</composite:interface>

<!--implementation-->
<composite:implementation>
    <li><h:commandLink action="#{cc.attrs.view}" value="#{cc.attrs.title}" /></li>
</composite:implementation>

How can I get an action String into the action attribute of the <h:commandLink>?

Answer

cayhorstmann picture cayhorstmann · Jun 24, 2010

Looks like this attracts the Horstmanns :-)

You must name the attribute "action" and use retargeting. Then some special handling kicks in that is described with exquisite clarity (not) at

http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/composite/attribute.html

and the API doc of ViewDeclarationLanguage.retargetMethodExpressions (not ViewHandler) whose link I am not allowed to paste in.

Here is how you do it.

<composite:interface>
    <composite:attribute name="title" required="true" type="java.lang.String"/>
    <composite:attribute name="action" targets="view" required="true" />
</composite:interface>

<!--implementation-->
<composite:implementation>
    <li><h:commandLink id="view" value="#{cc.attrs.title}" /></li>
</composite:implementation>