Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 414
user316146 Avatar asked Apr 30 '13 21:04

user316146


People also ask

How do you call an anonymous class in Java?

Object = new Example() { public void display() { System. out. println("Anonymous class overrides the method display()."); } }; Here, an object of the anonymous class is created dynamically when we need to override the display() method.

Which of the following is an anonymous inner class instance?

Explanation: D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable.

What is the inner and anonymous inner class?

It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

Which of the following is true about anonymous inner class?

18) Which of the following is true about the anonymous inner class? Explanation: Anonymous inner classes are the same as the local classes except that they don't have any name. The main use of it is to override methods of classes or interfaces.


1 Answers

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/)

like image 113
Sergej Raishin Avatar answered Oct 02 '22 20:10

Sergej Raishin