How does a composite component set a property in it's client's backing bean?
Asked Answered
L

2

6

I have a composite component with an interface that contains this:

<cc:attribute name="model"
                  shortDescription="Bean that contains Location" >
        <cc:attribute name="location" type="pkg.Location"
                      required="true" />
    </cc:attribute>
</cc:interface>

So I can access the Location object in the markup with #{cc.attrs.model.location}.

I also access that object from the backing bean of the composite component like this:

    FacesContext fc = FacesContext.getCurrentInstance();
    Object obj = fc.getApplication().evaluateExpressionGet(fc, 
            "#{cc.attrs.model.location}", Location.class);

So now my composite component has done its work -- how do I call the setter method on the model from the backing bean? (i.e. model.setLocation(someValue) ?

Lexie answered 23/8, 2011 at 13:38 Comment(0)
C
8

Use ValueExpression#setValue().

FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExpression = facesContext.getApplication().getExpressionFactory()
    .createValueExpression(elContext, "#{cc.attrs.model.location}", Location.class);

valueExpression.setValue(elContext, newLocation);

The Application#evaluateExpressionGet() by the way calls ValueExpression#getValue() under the covers, exactly as described by its javadoc (if you have ever read it...)


Unrelated to the concrete problem, are you aware about the possibility to create backing UIComponent class for the composite component? I bet that this is much easier than fiddling with ValueExpressions this way. You could then just use the inherited getAttributes() method to get the model.

Model model = (Model) getAttributes().get("model);
// ...

You can find an example in our composite component wiki page.

Coldblooded answered 23/8, 2011 at 15:18 Comment(2)
You are talking about using the @FacesComponent anotation, right? I had posted another question on this topic with a composite-component tag and I guess this answers that question.Lexie
Yes, that's right. See also the linked wiki. I haven't seen your other question. Pay attention to proper tagging. Folks here usually browse by (interesting) tags.Coldblooded
H
1

what about the attribute "default" ? It seam that it is not implemented when using the backing component implementation.

xhtml :

<composite:interface>
    <composite:attribute name="test" 
                         type="java.lang.Boolean" 
                         default="#{false}"/>
</composite:interface>
<composite:implementation >
    TEST : #{cc.attrs.test}
</composite:implementation >

Java backing implementation :

 testValue = (Boolean) getAttributes().get("test");

if the test attribute is set in the main xhtml no problem : both xhtml and java backing have the same value. But when not set the default value is only on xhtml : The html contains

TEST : false 

But testValue is null in backing

Hindenburg answered 1/8, 2013 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.