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

2024-06-20

我有一个复合组件,其接口包含以下内容:

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

这样我就可以访问Location标记中的对象#{cc.attrs.model.location}.

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

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

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


Use ValueExpression#setValue() http://download.oracle.com/javaee/6/api/javax/el/ValueExpression.html#setValue%28javax.el.ELContext,%20java.lang.Object%29.

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() http://download.oracle.com/javaee/6/api/javax/faces/application/Application.html#evaluateExpressionGet%28javax.faces.context.FacesContext,%20java.lang.String,%20java.lang.Class%29顺便说一句ValueExpression#getValue() http://download.oracle.com/javaee/6/api/javax/el/ValueExpression.html#getValue%28javax.el.ELContext%29在幕后,正如其所描述的那样javadoc http://download.oracle.com/javaee/6/api/javax/faces/application/Application.html#evaluateExpressionGet%28javax.faces.context.FacesContext,%20java.lang.String,%20java.lang.Class%29(如果你读过它......)


无关针对具体问题,您是否意识到创建支持的可能性UIComponent复合组件的类?我敢打赌这比摆弄要容易得多ValueExpression是这样的。然后你就可以使用继承的getAttributes()方法得到model.

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

您可以在我们的中找到示例复合组件 wiki 页面 https://stackoverflow.com/tags/composite-component/info.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

复合组件如何在其客户端的支持 bean 中设置属性? 的相关文章

随机推荐