How does a composite component set a property in it as a bean client support?

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> 

Therefore, I can access the Location object in the markup using # {cc.attrs.model.location} .

I also access this object from the bean base of the composite component as follows:

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

So now my composite component has done its job - how can I call the setter method on a model from a backup bean? (i.e. model.setLocation (someValue) ?

+6
source share
2 answers

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); 

Application#evaluateExpressionGet() by calling ValueExpression#getValue() under the covers, exactly as javadoc described (if you ever read this ...)


Unrelated to a specific problem, do you know about the possibility of creating a UIComponent support class for a composite component? I bet it's a lot easier than messing around with ValueExpression this way. Then you can use the inherited getAttributes() method to get the model .

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

You can find an example on our composite wiki page .

+6
source

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

xhtml:

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

Implementing Java Support:

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

if the test attribute is not set mainly xhtml without problems: both xhtml and java support have the same value. But when the default value is not set, it is only in xhtml: The html contains

 TEST : false 

But testValue is null in support

+1
source

Source: https://habr.com/ru/post/895678/


All Articles