繁体   English   中英

a4j:ajax侦听器异常MethodNotFoundException

[英]a4j:ajax listener exception MethodNotFoundException

我开始研究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中的“ listener”参数的文档中说:

该表达式必须计算为带ActionEvent参数且返回类型为void的公共方法,或者为不带参数返回值类型为void的公共方法。

为什么使用不带ActionEvent参数的countListener() 我不明白

为了使RF4可以使用listener属性,您的listener方法应采用AjaxBehaviorEvent类型的参数,而不是ActionEvent类型的参数。 从错误消息中可以看到的另一种替代方法是定义一个标准的Java方法,该方法不带参数,并且返回类型为void

   public void countListener();

为什么使用不带ActionEvent参数的countListener()? 我不明白

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

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

public void countListener(ActionEvent event) {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM