2013-11-02 71 views
0

我試圖在我的項目中實現一個帶有f:ajaxf:param的命令按鈕,但我沒有成功。CommandButton不適用於Ajax和Param - JSF 2.0

我有一個名爲「disciplinaMBean」的Bean有一個@SessionScoped。在各自的JSF(比方說,index.xhtml)我有一個p:dataTable顯示一些學科(這是什麼「訓練」的英文意思是:P)。當我點擊一行時,會打開一個模式,向用戶顯示「編輯」或「刪除」紀律。所選對象通過 dataTable的selection屬性存儲在「disciplinaSelecionada」中。 自己看:

"Nome" means Name. "Salvar" means Save and "Excluir" means delete

這個模式的代碼是內部 「edit.xhtml」 文件。 請參見下面的代碼:

<h:form> 
    <label for="nome">Nome:</label> 
    <h:inputText styleClass="form-control adicionar" id="nome" 
      value="#{disciplinaMBean.disciplinaSelecionada.nome}"> 
    </h:inputText> 

     <h:commandButton id="excluir" styleClass="btn btn-md btn-danger pull-left" value="Excluir" > 
     <f:param name="idDisciplina" value="#{disciplinaSelecionada.id}" /> 
      <f:ajax event="click" listener="#{disciplinaMBean.deletar}" /> 
    </h:commandButton> 
</h:form> 

與方法「Deletar」(指刪除)在disciplineMBean:

public void deletar(){ 
    String param1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idDisciplina"); 
    System.out.println("Disciplina deletada: " + param1); 
    int idDisciplina = Integer.parseInt(param1); 
    controleDisciplina.remover(idDisciplina); 
} 

我知道我所謂的方法,因爲日誌的控制檯,但我可以」 t刪除紀律,因爲我有一個錯誤(一個空字符串存儲在「idDisciplina」參數)。


以下是錯誤:

Disciplina deletada: 
Nov 01, 2013 10:26:31 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute 
WARNING: /edit.xhtml @31,72 listener="#{disciplinaMBean.deletar}": java.lang.NumberFormatException: For input string: "" 
javax.el.ELException: /edit.xhtml @31,72 listener="#{disciplinaMBean.deletar}": java.lang.NumberFormatException: For input string: "" 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111) 
    at com.sun.faces.facelets.tag.jsf.core.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxHandler.java:447) 
    at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113) 
    at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:760) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:724) 
Caused by: java.lang.NumberFormatException: For input string: "" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:592) 
    at java.lang.Integer.parseInt(Integer.java:615) 
    at mbeans.DisciplinaMBean.deletar(DisciplinaMBean.java:143) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:491) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    ... 27 more 

我能做些什麼來解決這個問題?

回答

1

如果你有EL 2.2,你可以做這樣的(假設disciplinaSelecionada.idint

<h:commandButton id="excluir" action="#{disciplinaMBean.deletar(disciplinaSelecionada.id)}" styleClass="btn btn-md btn-danger pull-left" value="Excluir" /> 

....

public void deletar(int idDisciplina) 
{ 
    System.out.println("Disciplina deletada: " + idDisciplina); 
    controleDisciplina.remover(idDisciplina); 
} 
+0

謝謝,但是,對不起......什麼是EL 2.2? – Paladini

+0

我的老師告訴我JSF無法接收參數並且必須返回一個字符串,他是不對的? – Paladini

+0

@FernandoPaladini我可以問你正在使用哪個操作系統?我喜歡那個 –