简体   繁体   中英

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

and a simple 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++;
    }
}

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 countListener() without ActionEvent 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.

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

public void countListener(ActionEvent event) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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