2014-09-11 94 views
-2

我知道這個問題是重複的,但沒有任何回覆幫助我解決這個問題。 我想用commandButton和confirmDialog從primefaces數據表中刪除一行,這不起作用,因爲backBean沒有獲取值。當我試圖刪除這個顯示我一個空指針異常。如果你能幫助我,請看我的代碼。我無法從數據表中刪除一行數據表

這是我的XHTML:

<h:form id="principalForm"> 
     <p:menubar> 
      <p:menuitem value="Nuevo" icon="ui-icon-plusthick" oncomplete="PF('wdlgIngresar').show();"/> 
     </p:menubar> 
     <p:dataTable id="dtpacientes" value="#{beanPersona.listPersona}" var="per" 
        paginator="true" rows="15" rowsPerPageTemplate="15,20,30" paginatorPosition="bottom" 
        paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
        selectionMode="single" selection="#{beanPersona.currPersona}" rowKey="#{per.id}"> 

      <p:ajax event="rowSelect" listener="#{beanPersona.onRowSelect}" update=":dlgVer" oncomplete="PF('wdlgVer').show()" /> 

      <p:column headerText="Id" sortBy="#{per.id}" filterBy="#{per.id}"> 
       <h:outputText value="#{per.id}" /> 
      </p:column> 
      <p:column headerText="Nombre" sortBy="#{per.nombres} #{per.apellidos}" filterBy="#{per.nombres} #{per.apellidos}"> 
       <h:outputText value="#{per.nombres} #{per.apellidos}" /> 
      </p:column> 
      <p:column headerText="Correo" sortBy="#{per.correo}" filterBy="#{per.correo}"> 
       <h:outputText value="#{per.correo}" /> 
      </p:column> 
      <p:column headerText="Edad" > 
       <h:outputText value="#{beanPersona.getEdad(per.nacimiento)}" /> 
      </p:column> 
      <p:column headerText="Sexo" > 
       <h:outputText value="#{beanPersona.getSexo(per.sexo)}" /> 
      </p:column> 
      <p:column id="actions" headerText="Acciones"> 
       <p:commandButton title="Borrar" icon="ui-icon-trash" update=":principalForm:deleteConfirmDialog" 
           oncomplete="PF('deleteConfirmation').show();" actionListener="#{beanPersona.nullMethod}"> 
        <f:setPropertyActionListener value="#{per}" target="#{beanPersona.currPersona}" /> 
       </p:commandButton> 
      </p:column> 
     </p:dataTable> 
     <!-- HERE beanPersona.currPersona.id HAVE THE CURRENT VALUE CORRECTLY--> 
     <p:confirmDialog id="deleteConfirmDialog" header="Atencion!" severity="alert" widgetVar="deleteConfirmation" 
         message="Desea eliminar paciente Nº #{beanPersona.currPersona.id} ?" > 
      <p:commandButton id="confirm" value="Sí" oncomplete="PF('deleteConfirmation').hide();" 
          update=":principalForm:dtpacientes" action="#{beanPersona.eliminar(beanPersona.currPersona)}" /> 
      <p:commandButton id="decline" value="No" oncomplete="PF('deleteConfirmation').hide();"/> 
     </p:confirmDialog> 
    </h:form> 

這是我的豆:

@ManagedBean 
@ViewScoped 
public class BeanPersona implements Serializable { 

    private Persona persona = new Persona(); 
    private Persona currPersona = new Persona(); 

    public BeanPersona() { 
     persona = new Persona(); 
     currPersona = new Persona(); 
    } 

    public Persona getCurrPersona() { 
     return currPersona; 
    } 

    public void setCurrPersona(Persona currPersona) { 
     this.currPersona = currPersona; 
    } 

    public Persona getPersona() { 
     return persona; 
    } 

    public void setPersona(Persona persona) { 
     this.persona = persona; 
    } 

    public void eliminar(Persona currPersona) { 
     if(currPersona != null) { 
      new Dml().delete(currPersona, currPersona.getId()); 
      listar(); 
      System.out.println("succefull delete !!!!"); 
     } else if(this.currPersona != null){ 
      new Dml().delete(this.currPersona, this.currPersona.getId()); 
      this.currPersona = new Persona(); 
      listar(); 
      System.out.println("succefull delete this.currPersona!!!!"); 
     } else { 
      System.out.println("currPersona is null !!!!"); 
     }    
    } 

    public String nullMethod() {return null;} 

} 

currPersona仍空當我扔的方法

+0

那麼bean的範圍是什麼? NPE在哪裏拋出? – 2014-09-11 06:18:16

+0

範圍是ViewScooped,請參閱更新。當執行消除方法時,NPE被拋出,因爲它說currPersona爲空。 – 2014-09-11 11:56:57

回答

1

我解決了它,我只是把過程標籤爲「確認」的commandButton與價值的「@this」

  <p:column id="actions" headerText="Acciones"> 
       <p:commandButton title="Borrar" icon="ui-icon-trash" update=":principalForm:deleteConfirmDialog" 
           oncomplete="PF('deleteConfirmation').show();" actionListener="#{beanPersona.setCurrPersona(per)}"> 
        <f:setPropertyActionListener value="#{per}" target="#{beanPersona.currPersona}" /> 
       </p:commandButton> 
      </p:column> 
     </p:dataTable> 
     <!-- HERE beanPersona.currPersona.id HAVE THE CURRENT VALUE CORRECTLY--> 
     <p:confirmDialog id="deleteConfirmDialog" header="Atencion!" severity="alert" widgetVar="deleteConfirmation" 
         message="Desea eliminar a paciente Nº #{beanPersona.currPersona.id} ?" closeOnEscape="true" > 
      <p:commandButton id="confirm" value="Sí" oncomplete="PF('deleteConfirmation').hide();" 
          update=":principalForm:dtpacientes" action="#{beanPersona.eliminar}" process="@this"/> 
      <p:commandButton id="decline" value="No" oncomplete="PF('deleteConfirmation').hide();"/> 
     </p:confirmDialog> 
1

屬性onclick是客戶端的行爲像執行一些JS或顯示一個對話框等等。你沒有爲你的commandButton定義一個actionListener。

嘗試將其完全刪除onclick和使用合適的替代ActionListener的:

<p:column id="actions" headerText="Acciones"> 
    <p:commandButton title="Borrar" icon="ui-icon-trash" 
     update=":principalForm:deleteConfirmDialog" 
     oncomplete="PF('deleteConfirmation').show();" 
     actionListener="#{beanPersona.someActionListener}"> 
       <f:setPropertyActionListener value="#{per}" 
        target="#{beanPersona.currPersona}" /> 
    </p:commandButton> 
</p:column> 
+0

,因爲動作監聽器在confirmDialog之後,commandButton只顯示confirmDialog,並用f:setPropertyActionListener將值設置爲currPersona – 2014-09-11 12:04:27

+0

您必須發送*兩個*請求。一個設置你的值'currPersona',然後在另一個對話框中執行刪除操作。對於第一個actionListener,你可以調用一個'dummy',它不會像'public String someActionListener(){return null}'這樣的東西。也許一個醜陋的黑客,但它的作品。 – stg 2014-09-11 12:25:21

+0

我做了你所說的,但仍然沒有工作。我做了一個不執行任何操作的方法,也嘗試將onclick中的值傳遞給actionListener,但沒有任何操作。 – 2014-09-11 18:40:07