0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

f:ajaxでMethod not foundとなった時の対応方法

Posted at

事象 : f:ajaxを動かそうとしたらコンソールにエラーが出て動かなかった

  • 環境
    • 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のコンソールのログ
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)
...省略...
UploadBean.java
...省略...
    public void uploadFile(FacesContext fc, UIComponent uic, Object value) throws IOException {
        if (!isUpload()) {
...省略...
base.xhtml
...省略...
<f:ajax event="change" execute="uploadArea" render="uploadArea" listener="#{uploadBean.uploadFile}" />
...省略...

原因 : メソッドのパラメータが違うから(オーバロード)

「メソッドのパラメータが違う」 = 「違うメソッド」 = 「listener属性に指定したメソッドはない」
いろいろ試していたら・・・うっかり・・・よく考えるとあたりまえだけど「えーーー?」っとなった

対応方法1 : メソッド名だけじゃなくて引数までlistener属性と合わせる

UploadBean.java
    public void uploadFile() throws IOException {
// または
    public void uploadFile(AjaxBehaviorEvent event) throws IOException {

javax.faces.event.AjaxBehaviorEventを受け取っておけばgetFacesContextでFacesコンテキストを取得できたりする。

AjaxBehaviorEvent は、Ajax に固有のコンポーネントの動作を表します)。
AjaxBehaviorEvent (Jakarta EE 8 仕様 API) - Javadoc 日本語訳

対応方法2 : listener属性に指定したメソッドに引数を追加する

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?