Wizzard Wizzard - 8 months ago 53
Java Question

a4j:ajax listener exception MethodNotFoundException

I started study RichFaces 4.2.2 and have a problem in simple example, I have an xml:

<ui:define name="content">
<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:outputText value="Echo:"/>
<h:outputText id="echo" value="#{echoBean.name}"/>

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


and a simple bean:

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

//getter setter methods here

public void countListener(ActionEvent event) {

And when i try to print in inputText i have exception:

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

But why? With button this same listener works just fine and in docs for "listener" parameter in a4j:ajax it says that:

The expression must evaluate to a public method that takes an ActionEvent parameter, with a return type of void, or to a public method that takes no arguments with a return type of void

Why it uses
parameter? I don't get it.


For you to be able to use the listener attribute with RF4, your listener method should take an argument of the AjaxBehaviorEvent type, not an ActionEvent type. The other alternative approach as you can see from the error message is to define a standard java method that doesn't take arguments and has a void return type as in

   public void countListener();

Why it uses countListener() without ActionEvent parameter? I don't get it.

That's the contract for the API, you're required to conform to be able to use it.