【问题标题】:a4j:ajax listener exception MethodNotFoundExceptiona4j:ajax 监听异常 MethodNotFoundException
【发布时间】:2012-10-03 03:23:28
【问题描述】:

我开始学习 RichFaces 4.2.2 并在简单示例中遇到问题,我有一个 xml:

<ui:define name="content">       
        <h:form>
            <rich:panel style="width: 50%">
                <h:panelGrid columns="2">
                    <h:outputText value="Name:"/>
                    <h:inputText id="inp" value="#{echoBean.name}">
                        <a4j:ajax event="keyup" render="echo count"  listener="#{echoBean.countListener}"/>
                    </h:inputText>

                    <h:outputText value="Echo:"/>
                    <h:outputText id="echo" value="#{echoBean.name}"/>

                    <h:outputText value="Count:"/>
                    <h:outputText id="count" value="#{echoBean.count}"/>
                </h:panelGrid>
                <a4j:commandButton value="Submit" actionListener="#{echoBean.countListener}" render="echo, count"/>
            </rich:panel>
        </h:form>

</ui:define>

还有一个简单的 bean:

@Component("echoBean")
@Scope(value = "session")
public class EchoBean {
private String name;
private Integer count = 0;

//getter setter methods here

public void countListener(ActionEvent event) {
    count++;
    }
}

当我尝试在 inputText 中打印时出现异常:

Caused by: javax.el.MethodNotFoundException: /home.xhtml @35,112 listener="#{echoBean.countListener}": Method not found: com.example.training.bean.EchoBean@d523fa.countListener()
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at org.ajax4jsf.component.behavior.MethodExpressionAjaxBehaviorListener.processAjaxBehavior(MethodExpressionAjaxBehaviorListener.java:71)
at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:98)
at org.ajax4jsf.component.behavior.AjaxBehavior.broadcast(AjaxBehavior.java:348)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:763)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
... 19 more

但是为什么呢?使用按钮,这个相同的监听器工作得很好,在 a4j:ajax 中的“监听器”参数的文档中它说:

表达式必须计算为接受 ActionEvent 参数、返回类型为 void 的公共方法,或计算为不接受任何参数且返回类型为 void 的公共方法

为什么它使用countListener() 没有ActionEvent 参数?没看懂。

【问题讨论】:

    标签: java ajax jsf listener ajax4jsf


    【解决方案1】:

    为了能够将listener 属性与RF4 一起使用,您的侦听器方法应采用AjaxBehaviorEvent 类型的参数,而不是ActionEvent 类型的参数。从错误消息中可以看出,另一种替代方法是定义一个标准的 java 方法,该方法不接受参数并且具有 void 返回类型,如

       public void countListener();
    

    为什么它使用 countListener() 没有 ActionEvent 参数?没看懂。

    这是 API 的合同,您必须遵守才能使用它。

    【讨论】:

    • 谢谢,我已经阅读了有关 Faces ActionEvent 的信息,但没有找到有关 AjaxBehaviourEvent 的任何信息
    【解决方案2】:

    使用带有以下签名的bean函数 void 作为返回类型 ActionEvent 对象作为参数 bean函数示例如下

    public void countListener(ActionEvent event) {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 2015-06-17
      • 2013-07-27
      相关资源
      最近更新 更多