invoke methods with parameter(s) on anonymous inner class beans in EL
Asked Answered
K

2

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.

Karlotte answered 30/4, 2013 at 21:42 Comment(2)
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.Phototaxis
reference: #1300337Dropline
O
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/)

Orelle answered 14/5, 2013 at 1:56 Comment(1)
With an instance of a regular class, it works with any number of parametersDropline
R
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)}
Ruckus answered 16/5, 2013 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.