我需要以编程方式调用/调用我的备份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.getValueExpresssion("value").getExpressionString()返回字符串:
#{reports.customer}
关于我所调用的bean的信息,(不知道这些信息是否相关):
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
我试图调用的方法:
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: com.npp.beans.reports.SharebackReportsBean@1ebf0d3.customer()
javax.faces.el.EvaluationException: javax.el.MethodNotFoundException: Method not found: com.npp.beans.reports.SharebackReportsBean@1ebf0d3.customer()
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: com.npp.beans.reports.SharebackReportsBean@1ebf0d3.customer()
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: com.npp.beans.reports.SharebackReportsBean@1ebf0d3.customer()
javax.faces.FacesException: #{reports.exportToExcel}: javax.el.MethodNotFoundException: Method not found: com.npp.beans.reports.SharebackReportsBean@1ebf0d3.customer()
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)
在这方面的任何帮助都是非常感谢的!
发布于 2011-03-23 18:58:30
您正在尝试将值表达式视为方法表达式。这不管用。值表达式指向getter方法(因此可以省略get
),而方法表达式指向操作方法,可能需要额外的参数。既然您已经拥有了ValueExpression
,就直接从它获得值,而不是试图把它当作一个MethodExpression
来对待。
Result result = (Result) data.getValueExpression("value").getValue(elc);
不应更改视图中的EL字符串。就留着吧,value="#{reports.customer}"
。否则,它将无法在正常的视图中工作。
https://stackoverflow.com/questions/5409420
复制相似问题