我无法从数据表的primefaces中删除一行

时间:2014-09-11 03:32:51

标签: jsf-2 primefaces datatable

我知道这个问题是重复的,但没有一个回复帮助我解决这个问题。 我尝试使用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>

这是我的Bean:

@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仍为null

2 个答案:

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

答案 1 :(得分:1)

我解决了它,我只是将过程标签放入&#34;确认&#34; commandButton的值为&#34; @ this&#34;

            <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>