Domanda

Ho bisogno di chiamare / invocare programmaticamente un metodo in uno dei miei backing bean.Ho esaminato diversi esempi e da quello che posso vedere, questo "dovrebbe" funzionare.

Il mio codice:

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 () restituisce la stringa:

#{reports.customer}

Informazioni sul bean che sto chiamando ( non so se sono rilevanti ):
Il nome del bean gestito della classe è "report"
La classe è nell'ambito della sessione
Class implementa
serializzabile Il modificatore di accesso del metodo che sto chiamando è
Non ci sono parametri nella firma del metodo

Metodo che sto cercando di invocare:

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

Estratto Stack-Trace

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)

Qualsiasi aiuto su questo è molto apprezzato!

È stato utile?

Soluzione

Stai tentando di trattare un'espressione di valore come un'espressione di metodo.Non funzionerà.Le espressioni di valore puntano a metodi getter (e quindi get può essere omesso) mentre le espressioni di metodo puntano a metodi di azione, eventualmente prendendo un argomento in più.Dal momento che hai già il ValueExpression, puoi ricavarne valore direttamente invece di tentare di trattarlo come un MethodExpression.

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

Non dovresti cambiare la stringa EL nella vista.Tienilo semplicemente value="#{reports.customer}".Altrimenti non funzionerà nella visualizzazione normale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top