2011-08-23 27 views
6

我有一個複合材料部件與包含此接口:複合組件如何在其客戶端的後臺bean中設置屬性?

<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訪問位置對象的標記。

我也是從合成成分的支持bean這樣訪問該對象:

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

所以現在我的複合材料部件完成了它的工作 - 我該如何調用setter方法對模型從支持bean? (即model.setLocation(someValue中)

回答

6

使用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描述(如果你曾經看過。 ..)


無關的具體問題,你是否知道有關的可能性,爲複合組件創建後備UIComponent類?我敢打賭,這比用這種方法擺弄ValueExpression要容易得多。您可以使用繼承的getAttributes()方法獲取model

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

你可以在我們的composite component wiki page一個例子。

+0

你說的是使用@FacesComponent anotation測試值是零,對吧?我用複合組件標籤在這個主題上發佈了另一個問題,我想這回答了這個問題。 – AlanObject

+0

是的,沒錯。另請參閱鏈接的wiki。我還沒有看到你的其他問題。注意適當的標籤。這裏的人們通常會瀏覽(有趣的)標籤。 – BalusC

1

屬性「default」呢?它在使用支持組件實現時沒有實現。

XHTML:

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

Java的底層實現:

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

如果測試屬性在主XHTML設置沒有問題:既XHTML和java的支持具有相同的值。但是,當不設置默認值僅適用於XHTML: HTML中包含

TEST : false 

但後盾

相關問題