本文介绍了复合组件如何在其客户的后备bean中设置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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

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

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

因此,现在我的复合组件已经完成了工作-如何从后备bean调用模型上的setter方法? (即 model.setLocation(someValue)吗?

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

推荐答案

使用 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() 通过调用 ValueExpression#getValue() 完全由其 javadoc (如果您曾经阅读过……)

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

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

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

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

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

这篇关于复合组件如何在其客户的后备bean中设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:28