2011-06-15 184 views
11

標題真的說了一切。 我已經失敗並錯誤的嘗試:將參數傳遞到複合組件動作屬性

Illegal attempt to pass arguments to a composite component lookup expression (i.e. cc.attrs.[identifier]).

我的嘗試是這樣的:

<composite:interface> 
    <composite:attribute name="removeFieldAction" method-signature="void action(java.lang.String)" /> 
</composite:interface> 
<composite:implementation> 
    <h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction('SomeString')}"/> 
</composite:implementation> 

什麼是做到這一點的正確方法?

回答

29

這確實不起作用。之後你不能再傳遞「額外」的參數。您已聲明的method-signature必須在使用複合組件的一側實現。例如。

<my:button action="#{bean.remove('Somestring')}" /> 

複合組件實現應該只是看起來像這樣

<h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction}" /> 

如果這不是你想要什麼,你真的要在複合材料部件邊通過它,那麼我可以請考慮傳遞額外參數的兩種方法:使用<f:attribute>與動作偵聽器將其作爲attidional組件屬性傳遞,或者使用<f:setPropertyActionListner>讓JSF在調用動作之前將其設置爲屬性。但是,兩者都沒有改變複合組件。您需要至少請求整個bean作爲複合組件的一個屬性。

下面是<f:setPropertyActionListener>的示例。這在調用動作之前設置屬性。

<composite:interface> 
    <composite:attribute name="bean" type="java.lang.Object" /> 
    <composite:attribute name="action" type="java.lang.String" /> 
    <composite:attribute name="property" type="java.lang.String" /> 
</composite:interface> 
<composite:implementation> 
    <h:commandButton value="Remove" action="#{cc.attrs.bean[cc.attrs.action]}"> 
     <f:setPropertyActionListener target="#{cc.attrs.bean[cc.attrs.property]}" value="Somestring" /> 
    </h:commandButton> 
</composite:implementation> 

將被用作

<my:button bean="#{bean}" action="removeFieldAction" property="someString" /> 

通過上面的例子,這個bean應該像

public class Bean { 

    private String someString; 

    public void removeFieldAction() { 
     System.out.println(someString); // Somestring 
     // ... 
    } 

    // ... 
} 

如果你堅持一個具體的約定,你可以甚至省略完全屬於property屬性。

+0

Tnx。 想法是複合組件創建一個字段列表,每個字段鏈接到bean中的一個對象。一旦從UI中刪除一個字段,必須通知bean已刪除字段的ID,以便它也將從bean中刪除。因此,'someString'參數實際上是被刪除字段的UUID。 我在實踐中試圖用一個參數來實現類似於事件監聽器的事情...... 感謝您的解決方案! – Ben 2011-06-15 12:25:01

+1

Hi BalusC。非常感謝你的回覆。這適用於Mojarra,但似乎不適用於MyFaces。我在這裏發佈了一個單獨的問題,請你有空看看我有空嗎? http://stackoverflow.com/questions/17357593/passed-argument-to-method-inside-composite-component-does-not-work-on-myfaces非常感謝你 – 2013-06-28 05:09:48