9

If I have an anonymous inner class object like this (where Foo is an interface):

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

and I try to call Foo.hello from a jsp like this:

${foo.hello('blah')}

it throws:

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

but if there are no parameters:

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

...

${bar.hello()}

it works fine. Why?

This is not a duplicate of 7121303. I'm asking specifically about anonymous inner classes. With an instance of a regular class, it works with any number of parameters.

Community
  • 1
  • 1
user316146
  • 1,307
  • 1
  • 12
  • 19
  • I think I remember having been bitten by this kind of bu on an old version of Tomcat. What's your server? Make sure you're using the latest version. – JB Nizet May 10 '13 at 14:27
  • reference: http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen – Rong Nguyen May 14 '13 at 02:04

2 Answers2

1

Possibly, you need to create EL function though which you can pass parameter. (http://blog.idleworx.com/2010/04/custom-tags-and-custom-el-functions-in.html)

the support for passing method arguments and invoking non-getter methods was introduced in EL 2.2 . Enable EL 2.2 on tomcat (http://code2inspire.wordpress.com/2010/11/05/how-to-enable-el-2-2-on-tomcat-6/)

Sergej Raishin
  • 535
  • 4
  • 7
1

I do not know which environment you are using but I tried on tomcat7.0.40 and your code works fine.

One possibility is that there might be issue with passing String when Object is expected. May be some strict parsing. Can you try following: Store the argument to pageContext and use that to pass value to function as follows.

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

${foo.hello(argObj)}
Snehal Patel
  • 819
  • 6
  • 9