2011-12-07 44 views
5

我需要一些幫助來獲取我的複合組件的行爲。我仍然在學JSF中的繩索,所以請原諒可能在這篇文章中顯示的任何無知。JSF2.0複合組件actionListener

因此,我正在使用JSF 2.0,並決定構建一個複合組件來使這個特定的代碼片段可重用。該組件完美地顯示了一切,但在嘗試添加actionListener時完全不會表現出來。

這裏是我的組件的代碼

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:composite="http://java.sun.com/jsf/composite"> 

    <composite:interface displayName="columnUpdate"> 
     <composite:attribute name="id" 
      required="true" 
      displayName="id" 
      shortDescription="Id to apply to this control and its child components"> 
     </composite:attribute> 
     <composite:attribute 
      name="value" 
      displayName="value" 
      required="true" 
      shortDescription="Field that will hold the new updated value."> 
     </composite:attribute> 

     <composite:attribute 
      name="columnName" 
      displayName="columnName" 
      required="true" 
      shortDescription="Value that will be applied as the column header"> 
     </composite:attribute> 
     <composite:attribute 
      name="text" 
      displayName="text" 
      shortDescription="Text to be displayed above the field"> 
     </composite:attribute> 

     <composite:actionSource name="actionEvent" targets="saveEvent"></composite:actionSource> 
    </composite:interface> 

    <composite:implementation> 
     <h:outputLabel value="#{cc.attrs.columnName}" onclick="showWindow('#{cc.attrs.id}')"></h:outputLabel> 

     <div id="#{cc.attrs.id}" class="ccColumnUpdate"> 
     <div id="windowClose" onclick="closeWindow('#{cc.attrs.id}');" style="top: -10px;" title="Close this window">CLOSE</div> 
      <div> 
       <h:outputLabel id="lblTitle" value="#{cc.attrs.text}"></h:outputLabel> 
      </div> 
      <div> 
       <h:inputText id="txtValue" value="#{cc.attrs.value}"></h:inputText> 
      </div> 
      <div style="text-align:right;"> 
       <h:commandButton id="saveEvent" value="Save"></h:commandButton> 
      </div> 
     </div> 
    </composite:implementation> 
</ui:composition> 

這裏是我的網頁使用該組件的代碼:

<al:columnUpdate id="cuExpenseAmount" value="#{expense.columnValue}" columnName="Expense Amount" text="Set Expense Amount to:"> 
    <f:actionListener for="actionEvent" binding="#{expense.updateColumnValue}"> 
    <f:ajax execute="@form"></f:ajax> 
    </f:actionListener> 
</al:columnUpdate> 

,這裏是我的支持bean代碼:

public void updateColumnValue(ActionEvent event) throws ParseException{ 
    //Got HERE 

} 

我沒有收到任何錯誤或打到我在輔助bean上設置的任何斷點。任何幫助,或正確的方向指針將不勝感激。

問候,

邁克

回答

9

我找到了答案周圍一點點,更多的搜索播放後。

更改到複合材料部件: FROM:

<composite:actionSource name="actionEvent" targets="saveEvent"></composite:actionSource> 

<h:commandButton id="saveEvent" value="Save"></h:commandButton> 

TO:

<al:columnUpdate id="cuExpenseAmount" value="#{expense.columnValue}" registerButtonActionListener="#{expense.updateColumnValue}" columnName="Expense Amount"> 
          </al:columnUpdate> 

最大的變化是定義:使用複合材料部件

<composite:attribute name="registerButtonActionListener" method-signature="void actionListener(javax.faces.event.ActionEvent)" /> 

<h:commandButton id="saveEvent" value="Save" actionListener="#{cc.attrs.registerButtonActionListener}"> 
        <f:ajax execute="@this" event="click"></f:ajax> 
       </h:commandButton> 

更改頁面複合屬性中的方法簽名。

問候,

邁克

0

我找到了一個替代解決方案,以你的。您的解決方案的缺點是您只能註冊一個偵聽器:actionListener屬性只能指向一種方法。

添加監聽器的方法如下(你幾乎擁有了正確的第一段代碼你寫):

<al:columnUpdate id="cuExpenseAmount" value="#{expense.columnValue}" columnName="Expense Amount" text="Set Expense Amount to:"> 
<f:actionListener for="actionEvent" binding="#{expense.updateColumnValue(ActionEvent)}"> 
    <f:ajax execute="@form"></f:ajax> 
</f:actionListener> </al:columnUpdate> 

注意該方法如何通過binding指出,需要一個ActionEvent參數。這是使用<f:actionlistener/>的要求。

使用此方法,可以將多個偵聽器註冊到複合組件提供的動作源。