Search code examples
jsfjsf-2.2composite-componentpassthrough-attributes

Is it possible to pass passthrough attributes in a custom composite component?


In JSF 2.2, I have a custom composite component which wraps a vanilla component (let's say a h:commandLink).

Is it possible to add passthrough attributes on my composite component and pass them back to the wrapped component?

Something like this (it's an example, don't bother about its uselessness):

Declaration of my:commandLink:

<cc:interface>
    <cc:attribute name="text" type="java.lang.String" />
</cc:interface>

<cc:implementation>
    <h:commandLink a:magicInsertionOfAllThePassThroughAttributes>
        #{cc.attrs.text}
    </h:commandLink>
</cc:implementation>

Use of my:commandLink:

<my:commandLink text="oh no" a:data-something="…" a:data-otherthing="…" />

Solution

  • So, following @Kukeltje comments, I managed to do something with a custom composite component backing class:

    @FacesComponent("PassThrough")
    public class PassThroughComponent extends Component
    {
        @Override
        public void encodeBegin(FacesContext facesContext) throws IOException
        {
            super.encodeBegin(facesContext);
            findComponent("pass").getPassThroughAttributes().putAll(getPassThroughAttributes());
        }
    }
    

    With a DOM element bearing a id='pass' in my component, I can pass the attributes to it.

    Alas, Eclipse doesn't understand the trick, and triggers warnings seeing all these "unknown attribute 'a:something'".

    I truly wonder how Primefaces components manage to avoid that.