复合组件如何在其客户端的辅助bean中设置属性?

Ala*_*ect 6 composite-component jsf-2

我有一个复合组件,其接口包含:

<cc:attribute name="model"
                  shortDescription="Bean that contains Location" >
        <cc:attribute name="location" type="pkg.Location"
                      required="true" />
    </cc:attribute>
</cc:interface>
Run Code Online (Sandbox Code Playgroud)

所以我可以使用#{cc.attrs.model.location}访问标记中的Location对象.

我也从复合组件的支持bean访问该对象,如下所示:

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

所以现在我的复合组件已经完成了它的工作 - 如何从支持bean调用模型上的setter方法?(即model.setLocation(someValue)

Bal*_*usC 8

使用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);
Run Code Online (Sandbox Code Playgroud)

Application#evaluateExpressionGet()的方式调用ValueExpression#getValue()被窝里,正是因为其描述的Javadoc(如果你曾经阅读...)


具体问题无关,您是否了解UIComponent为复合组件创建支持类的可能性?我敢打赌,这比用ValueExpression这种方式摆弄要容易得多.然后你可以使用继承的getAttributes()方法来获取model.

Model model = (Model) getAttributes().get("model);
// ...
Run Code Online (Sandbox Code Playgroud)

您可以在我们的复合组件Wiki页面中找到一个示例.