【问题标题】:invoke methods with parameter(s) on anonymous inner class beans in EL在 EL 的匿名内部类 bean 中调用带参数的方法
【发布时间】:2013-04-24 22:31:30
【问题描述】:

如果我有一个像这样的匿名内部类对象(其中 Foo 是一个接口):

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

我尝试从这样的 jsp 调用 Foo.hello

${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 的副本。我专门询问匿名内部类。对于常规类的实例,它可以使用任意数量的参数。

【问题讨论】:

标签: java jsp el anonymous-inner-class


【解决方案1】:

您可能需要创建 EL 函数,通过该函数可以传递参数。 (http://blog.idleworx.com/2010/04/custom-tags-and-custom-el-functions-in.html)

在 EL 2.2 中引入了对传递方法参数和调用非 getter 方法的支持。在 tomcat 上启用 EL 2.2 (http://code2inspire.wordpress.com/2010/11/05/how-to-enable-el-2-2-on-tomcat-6/)

【讨论】:

  • With an instance of a regular class, it works with any number of parameters
【解决方案2】:

我不知道你使用的是哪个环境,但我在tomcat7.0.40上试过,你的代码运行良好。

一种可能性是,当期望 Object 时传递 String 可能会出现问题。可能是一些严格的解析。 您可以尝试以下操作: 将参数存储到 pageContext 并使用它来将值传递给函数,如下所示。

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

${foo.hello(argObj)}

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2013-10-19
    相关资源
    最近更新 更多