MethodExpression.invoke() 上未找到方法异常

发布于 2024-10-26 07:24:40 字数 5287 浏览 2 评论 0原文

我需要以编程方式调用/调用我的一个支持 bean 中的方法。我看过几个例子,据我所知,这个“应该”有效。

我的代码:

UIData data = (UIData)component;
fc = FacesContext.getCurrentInstance();
elc = fc.getELContext();

elFactory = fc.getApplication().getExpressionFactory();
mexp =
    elFactory.createMethodExpression(elc, data.getValueExpression("value").getExpressionString(), Result.class, new Class[]{});
Object methodResult = mexp.invoke(elc, null);

“data.getValueExpression("value").getExpressionString() 返回字符串:

#{reports.customer}

有关我正在调用的 bean 的信息不知道这些是否相关):
类的托管 bean 名称是“report”
类在会话范围内
类实现可序列化
我调用的方法的访问修饰符是
方法签名中没有参数

我尝试调用的方法:

public Result getCustomer() {
    Result result = null;
    try {
        ...perform database call
    } catch (Exception e) {
        log.error(e);
    }
    return result;
}

堆栈跟踪摘录

SEVERE: javax.el.MethodNotFoundException: Method not found: [email protected]()
javax.faces.el.EvaluationException: javax.el.MethodNotFoundException: Method not found: [email protected]()
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
    at javax.faces.component.UICommand.broadcast(UICommand.java:311)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
...
Caused by: javax.el.MethodNotFoundException: Method not found: [email protected]()
    at com.sun.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:155)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:231)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.npp.business.TableToExcelManager.initExcelWorker(TableToExcelManager.java:247)
    at com.npp.beans.reports.SharebackReportsBean.exportToExcel(SharebackReportsBean.java:439)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
    ... 26 more
Mar 23, 2011 11:29:34 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{reports.exportToExcel}: javax.el.MethodNotFoundException: Method not found: [email protected]()
javax.faces.FacesException: #{reports.exportToExcel}: javax.el.MethodNotFoundException: Method not found: [email protected]()
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
    at javax.faces.component.UICommand.broadcast(UICommand.java:311)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

非常感谢对此的任何帮助!

I have a need to programmatically call/invoke a method in one of my backing beans. I've looked at several examples, and from what I can see, this "should" work.

My code:

UIData data = (UIData)component;
fc = FacesContext.getCurrentInstance();
elc = fc.getELContext();

elFactory = fc.getApplication().getExpressionFactory();
mexp =
    elFactory.createMethodExpression(elc, data.getValueExpression("value").getExpressionString(), Result.class, new Class[]{});
Object methodResult = mexp.invoke(elc, null);

The "data.getValueExpresssion("value").getExpressionString() returns the string:

#{reports.customer}

Info about the bean I'm calling (don't know if these are relevant):
Class's managed bean name is "report"
Class is in Session-scope
Class implements Serializable
The access modifier of the method I'm calling is
There are no parameters in the method signature

Method I'm trying to invoke:

public Result getCustomer() {
    Result result = null;
    try {
        ...perform database call
    } catch (Exception e) {
        log.error(e);
    }
    return result;
}

Stack-Trace Excerpt

SEVERE: javax.el.MethodNotFoundException: Method not found: [email protected]()
javax.faces.el.EvaluationException: javax.el.MethodNotFoundException: Method not found: [email protected]()
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
    at javax.faces.component.UICommand.broadcast(UICommand.java:311)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
...
Caused by: javax.el.MethodNotFoundException: Method not found: [email protected]()
    at com.sun.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:155)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:231)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.npp.business.TableToExcelManager.initExcelWorker(TableToExcelManager.java:247)
    at com.npp.beans.reports.SharebackReportsBean.exportToExcel(SharebackReportsBean.java:439)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
    ... 26 more
Mar 23, 2011 11:29:34 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{reports.exportToExcel}: javax.el.MethodNotFoundException: Method not found: [email protected]()
javax.faces.FacesException: #{reports.exportToExcel}: javax.el.MethodNotFoundException: Method not found: [email protected]()
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
    at javax.faces.component.UICommand.broadcast(UICommand.java:311)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

Any help on this is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-11-02 07:24:40

您试图将值表达式视为方法表达式。这是行不通的。值表达式指向 getter 方法(因此可以省略 get),而方法表达式指向操作方法,可能需要一个额外的参数。由于您已经有了 ValueExpression,因此只需直接从中获取值即可,而不是尝试将其视为 MethodExpression

Result result = (Result) data.getValueExpression("value").getValue(elc);

您不应更改视图中的 EL 字符串。只需保留它value="#{reports.customer}"。否则在普通视图下将无法工作。

You're attempting to treat a value expression as a method expression. This isn't going to work. Value expressions points to getter methods (and thus get can be omitted) while method expressions points to action methods, possibly taking an extra argument. Since you already have the ValueExpression, just get value from it directly instead of attempting to treat it like a MethodExpression.

Result result = (Result) data.getValueExpression("value").getValue(elc);

You should not change the EL string in the view. Just keep it value="#{reports.customer}". Otherwise it won't work in the normal view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文