CommandButton不适用于Ajax和Param - JSF 2.0

时间:2013-11-02 00:45:50

标签: ajax jsf jsf-2 primefaces

我试图在我的项目中使用f:ajaxf:param实现一个命令按钮,但我没有成功。

我有一个名为“disciplinaMBean”的Bean,它有一个@SessionScoped。在相应的JSF(比方说,index.xhtml)中,我有一个p:dataTable显示了一些学科(这就是“门徒”在英语中的含义:P)。当我单击一行时,会打开一个模态,向用户显示“编辑”或“删除”规则的选项。所选对象通过Primefaces 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>

在discipMBean中的方法“Deletar”(意思是删除):

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);
}

我知道我调用该方法是因为控制台的日志,但我无法删除该规则,因为我有一个错误(空字符串存储在“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 个答案:

答案 0 :(得分: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);
}