2013-07-15 31 views
0

我有一個複合組件:複合組件 - 外部變化豆值

<cc:interface> 
    <cc:attribute name="value" required="true"> 
</cc:interface> 
<cc:implementation> 
    <h:outputText value="#{cc.attrs.value}"/> 
    <h:commandButton action="#{internalBean.someAction}"/> 
</cc:implementation> 

而且我想通過#{} internalBean.someAction改變#{} cc.attrs.value,換句話說:通過我的複合組件的方法更改用戶定義的(外部)bean的(String)值。我如何做到這一點?

謝謝。

+0

我不認爲你可以。 'h:outputText'需要一個值表達式,所以我不知道如何使用方法表達式。也許我不明白這個問題,所以你可以澄清。 – Andy

+0

好的,我會更精確地解釋一下:'value'是外部bean的屬性(fe:),'someAction()'是我的內部bean(在複合組件內)。我想在方法中獲得這個值並改變它。我可以用查找來做到這一點 - FacesContext ... –

+0

試圖在一秒內下墊 – Andy

回答

0

我終於找到了有史以來最好的解決方案。它立即作爲一個正常的組件 - 每一個變化更新外部bean屬性:

public void setValue(String value) { 
    this.value = value; 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ELContext elContext = facesContext.getELContext(); 
    ValueExpression valueExpression = facesContext.getApplication().getExpressionFactory() 
      .createValueExpression(elContext, "#{cc.attrs.value}", String.class); 
    valueExpression.setValue(elContext, value); 
} 
3

UPDATE

一個我能想到的方法是使用<f:setPropertyActionListener>

<cc:interface> 
    <cc:attribute name="value" required="true"/> 
</cc:interface> 
<cc:implementation> 
    <h:outputText value="#{cc.attrs.value}"/> 
    <h:commandButton action="#{internalBean.someAction}"> 
     <f:setPropertyActionListener value="#{cc.attrs.value}" target="#{internalBean.stringValueFromExternalBean}"/> 
    </h:commandButton> 
</cc:implementation> 
+0

非常感謝。但我擔心,我需要另一種解決方案,因爲我正在爲程序員開發一個複合組件。所以他們會實施外部bean,而不是我。這意味着我不能設置bean的類型和名稱。是否有可能通過@ManagedProperty以一般方式進行操作? –

+0

@PetrDušek我不確定。我可以試着看。問題是你想要將變化反映到另一個變化中,所以我想知道你如何知道對象類型。 – Andy

+0

我唯一需要的是:從外部bean傳遞String屬性並在內部操作它。這是允許的嗎?:@ManagedProperty(value =「#{cc.attrs.value}」) private String value; –

2

但它是沒有必要使用StringBuilder:

<composite:interface> 
     <composite:attribute name="value" required="true"/> 
    </composite:interface> 
<cc:implementation> 
... 
<f:setPropertyActionListener target="#{cc.attrs.value}" value="#{internalBean.value}"/> 
... 
</cc:implementation> 

如果值是正常的字符串。它工作正常!

+0

我很抱歉Petr。一路上我沒有看到整個畫面(** setter **被調用)。 BalusC解釋了爲什麼這在評論中起作用。忽略'StringBuilder'部分(但請記住,將來可以使用'type'來傳遞除String之外的對象)。非常感謝您指出這一點,並再次爲此感到抱歉。 – Andy

+0

沒問題,安迪,謝謝你的好評。再見! –