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

前端 未结 2 2062
再見小時候
再見小時候 2021-02-04 18:08

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



        
2条回答
  •  庸人自扰
    2021-02-04 18:58

    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.

提交回复
热议问题