[Java] How to respond when Method not found in f:ajax

1 minute read

Phenomenon: When I tried to run f:ajax, it did not work due to an error in the console

  • Environment -CentOS Linux release 7.8.2003 (Core) -Eclipse IDE for Enterprise Java Developers.Version: 2020-03 (4.15.0) -openjdk version “11.0.7” 2020-04-14 LTS -JSF 2.3.9

Eclipse console log


javax.el.MethodNotFoundException: /base.xhtml @17,113 listener="#{uploadBean.uploadFile}": Method not found: class brans.UploadBean.uploadFile(javax.faces.event.AjaxBehaviorEvent)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:69)
    at com.sun.faces.facelets.tag.jsf.core.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxHandler.java:403)
    at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:100)
    at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:82)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:481)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1395)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:58)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
    at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
...abridgement...

UploadBean.java


...abridgement...
    public void uploadFile(FacesContext fc, UIComponent uic, Object value) throws IOException {
        if (!isUpload()) {
...abridgement...

base.xhtml


...abridgement...
<f:ajax event="change" execute="uploadArea" render="uploadArea" listener="#{uploadBean.uploadFile}" />
...abridgement...

Cause: Method parameter is different (overload)

“Method parameter is different” = “Different method” = “No method specified in listener attribute” When I tried various things… I was inadvertently… It’s normal when I thought about it well

Solution 1: Match not only the method name but also the arguments with the listener attribute

UploadBean.java


    public void uploadFile() throws IOException {
// or
    public void uploadFile(AjaxBehaviorEvent event) throws IOException {

If you receive javax.faces.event.AjaxBehaviorEvent, you can get the Faces context with getFacesContext.

AjaxBehaviorEvent represents the behavior of the component that is specific to Ajax). AjaxBehaviorEvent (Jakarta EE 8 Specification API)-Javadoc Japanese Translation

Solution 2: Add an argument to the method specified in listener attribute

base.xhtml


<f:ajax event="change" execute="uploadArea" render="uploadArea" listener="#{uploadBean.uploadFile(hoge, fuga, value)}" />

Tags: ,

Updated: