primefaces rowediting datatable ejb update returns

2019-09-01 04:23发布

问题:

Salut :), iam a newbie in primefaces and ajax

Iam using primefaces 3.4, glassfish 3.1, jsf2.0 and ejb 3. I tried to implement primefaces showcase datatable rowediting. But when i validate the updated value into the datatable, i get the old value. This is my code :

<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:dataTable var="item" value="#{jSFMBean.allContacts}" id="contactList" editable="true">
    <p:ajax event="rowEdit" listener="#{jSFMBean.onEdit}" update="@this :form:messages" />
    <p:ajax event="rowEditCancel" listener="#{jSFMBean.onCancel}" update=":form:messages" />
        <p:column headerText="EMAIL" style="width:125px">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{item.email}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{item.email}" label="EMAIL"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column><p:column headerText="Options" style="width:50px"> 
                    <p:rowEditor />
                </p:column>
     </p:dataTable>
<h:outputText value="#{jSFMBean.selectedContact.displayname}" />

the methods are :

  public void onEdit(RowEditEvent event) {
    this.session.updateContact((Contacts) event.getObject());
    FacesMessage msg = new FacesMessage("Edition contact: ", ((Contacts) event.getObject()).getDisplayname());

    FacesContext.getCurrentInstance().addMessage(null, msg);
}

(Contacts) event.getObject() always get the old value :(. It's like the getter of the datatable fireup first before ajax update to the database.

what iam i doing wrong ? thank you for your help solving this .

回答1:

Currently you're editing values within the object. It looks like you still need to make a call back to your database to update the value there.



回答2:

It seems that whenever you need your dataTable you are getting it from your database and that's why event.getObject() always returns the old value.So in the getter of your datatable you need to add:

if (allContacts== null){

        allContacts= (List<Contacts>) yourservice.getAll();  /*this refers to the function that get the list from the database*/

    }

    return allContacts;

I hope that may help you.



回答3:

Most probably the problem is with your backing bean. if you have used @Named annotation instead of @ManagedBean (javax.faces.bean.ManagedBean) for your backing bean, you faces this kind of problems. Simply replace

@Named (value="YourBeanName")

with

@ManagedBean (name="YourBeanName")