JSF2.0复合组件actionListener

时间:2011-12-07 21:51:10

标签: jsf-2 actionlistener composite-component

我需要一些帮助才能让我的复合组件表现得更好。我仍然在学习JSF中的绳索,所以请原谅这篇文章中可能显示的任何无知。

所以,我正在使用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>

这是我在Backing Bean上的代码:

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

}

我没有收到任何错误或点击我在辅助bean上设置的任何断点。任何帮助,或正确方向的指示将不胜感激。

此致

麦克

2 个答案:

答案 0 :(得分:9)

我在玩了一下之后找到了答案。

复合组件的更改: FROM:

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

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

TO:

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

使用复合组件对页面进行更改:

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

最大的变化是在复合属性中定义方法签名。

此致

麦克

答案 1 :(得分: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/>

的要求

使用此方法,可以将多个侦听器注册到复合组件可用的操作源。

相关问题