【问题标题】:JSF2.0 Composite Component actionListenerJSF2.0 复合组件 actionListener
【发布时间】:2023-04-01 09:05:01
【问题描述】:

我需要一些帮助才能让我的复合组件正常运行。我还在学习 JSF 的基本原理,所以请原谅本文中可能显示的任何无知。

因此,我正在使用 JSF 2.0 并决定构建一个复合组件以使此特定代码 sn-p 可重用。该组件完美地显示了所有内容,但在尝试添加 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 上设置的任何断点。任何帮助或正确方向的指示将不胜感激。

问候,

迈克

【问题讨论】:

    标签: jsf-2 actionlistener composite-component


    【解决方案1】:

    经过一番反复搜索,我找到了答案。

    复合组件的变化: 来自:

    <composite:actionSource name="actionEvent" targets="saveEvent"></composite:actionSource> 
    
    <h:commandButton id="saveEvent" value="Save"></h:commandButton>  
    

    到:

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

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

    问候,

    迈克

    【讨论】:

      【解决方案2】:

      我找到了您的替代解决方案。您的解决方案的一个缺点是您只能注册一个侦听器: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 参数的。这是使用&lt;f:actionlistener/&gt;的要求。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 2012-10-30
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 2013-02-10
        • 1970-01-01
        • 2012-04-12
        相关资源
        最近更新 更多