SlimShady
SlimShady

Reputation: 185

Use of f:attribute inside a composite component

we have a (in our oppinion) very simple scenario here. But we got stuck somehow on composite components and f:attribute tags. I'll try to keep the code as simple as possible.

Composite Component:

<cc:interface name="button">
    ...
    <cc:attribute
        name="actionListener"
        required="true"
        method-signature="void f(javax.faces.event.ActionEvent)"
        target="button"
        shortDescription="The action listener for this button." />
    ...
</cc:interface>

<cc:implementation>
    <ice:commandButton
        ...
        actionListener="#{cc.attrs.actionListener}"
        ...>

        <cc:insertChildren />
    </ice:commandButton>
</cc:implementation>

... now we use the component like this:

<ctrl:button
    ...
    actionListener="#{bean.method}"
    ...>
    <f:attribute name="objectId" value="#{someObject.id}" />
</ctrl:button>

Now we need to access the "objectId" attribute inside the action listener method. We already tried somethign like this:

public void method(ActionEvent event)
{
    ...
    event.getComponent().getAttributes().get("objectId");
    ...
}

But the attribute map doesn't contain the objectId. Is there anything wrong on this approach? What is the recommended way to solve this problem?

Would be nice if someone could help us out.

Thanks! SlimShady

Upvotes: 4

Views: 4790

Answers (2)

BalusC
BalusC

Reputation: 1108722

This <f:attribute> hack is a leftover from JSF 1.0/1.1 when it was not possible to pass objects as additional arguments to command buttons/links. Since JSF 1.2 you are supposed to use <f:setPropertyActionListener> for this.

<ctrl:button action="#{bean.method}">
    <f:setPropertyActionListener target="#{bean.objectId}" value="#{someObject.id}" />
</ctrl:button>

Since EL 2.2 (which is standard part of Servlet 3.0 but is for Servlet 2.5 implementable with help of JBoss EL) you could instead even pass the whole object just as method argument:

<ctrl:button action="#{bean.method(someObject.id)}" />

Upvotes: 5

Dennis Bayer
Dennis Bayer

Reputation: 109

I managed to read an attribute passed into a cc within the following setup.

<test:inner>
    <f:attribute name="fAttribute" value="myAttributeValue" />
</test:inner>

<cc:implementation>
    <h:commandButton value="button" actionListener="#{testBean.actionListener}" >
        <f:attribute name="innerAttribute" value="innerAttributeValue" />
            <cc:insertChildren /> <!-- not necessary, I hoped it would pass the outer attribute --->
    </h:commandButton>
</cc:implementation>

public void actionListener(ActionEvent event) {
    event.getComponent().getNamingContainer().getAttributes().get("fAttribute") 
    // > myAttributeValue
    event.getComponent().getAttributes().get("innerAttribute") 
    // > innerAttributeValue
}

The trick was to search within the naming container of the button. Thus a cc is always a naming container you can be sure that you end up in the inner-component.

I'm not sure if that's the way it was intendet to be but as far as I figured out the naming containter collects such attributes for its children.

Q: Does anybody out there know if not passing the attributes into the button is considered as a bug within Mojarra/JSF?

Upvotes: 3

Related Questions