2013-04-30 34 views
9

如果我有一個匿名內部類對象是這樣的(其中,foo是一個接口):調用在EL與匿名內部類豆參數(一個或多個)方法

Foo foo = new Foo(){ 
    @Override 
    public String hello(Object dummyArg){ 
    return "hello, world."; 
    } 
}; 

和我嘗試調用Foo.hello從JSP是這樣的:

${foo.hello('blah')} 

它拋出:

javax.el.MethodNotFoundException: Unable to find method [hello] with [1] parameters 

,但如果沒有參數:

Bar bar = new bar(){ 
    @Override 
    public String hello(){ 
    return "hello, world."; 
    } 
}; 

...

${bar.hello()} 

它工作正常。爲什麼?

這不是7121303的重複。我正在具體詢問匿名內部類。使用常規類的實例,它可以處理任意數量的參數。

+0

我想我記得在老版本的Tomcat上被這種bu咬了。你的服務器是什麼?確保你使用的是最新版本。 – 2013-05-10 14:27:54

+0

參考:http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen – 2013-05-14 02:04:50

回答

1

我不知道你正在使用哪個環境,但我嘗試了tomcat7.0.40並且你的代碼工作正常。

一種可能性是當預期對象時傳遞String時可能存在問題。可能是一些嚴格的解析。 您可以試試以下內容: 將參數存儲到pageContext並使用它傳遞值以實現如下功能。

<% 
pageContext.setAttribute("argObj", "blah"); 
%> 

${foo.hello(argObj)} 
相關問題