row editing is not working in datatable primefaces

vijaya kumar picture vijaya kumar · Sep 19, 2013 · Viewed 15.4k times · Source

During editing of datatable values, the values are not updated in the screen as well as in listener method. The listener method:

public void onEdit(RowEditEvent event) throws ClassNotFoundException, SQLException
{
    Employee e=(Employee) event.getObject();
    name=e.getName();
    department=e.getDepartment();
    salary=e.getSalary();
    place=e.getPlace();
          ......
           ..... update query
}

I get only the selected object and its values, but not the updated values.

This is my datatable XHTML:

<h:form id="form">
    <p:growl id="messages" showDetail="true" />

    <p:dataTable var="e" value="#{employees.eList}" id="elist1"
        editable="true">
        <f:facet name="header">
        In-Cell Editing
    </f:facet>

        <p:ajax event="rowEdit" listener="#{employees.onEdit}"
            update=":form:messages" />
        <p:ajax event="rowEditCancel" listener="#{employees.onCancel}"
            update=":form:messages" />

        <p:column headerText="name" style="width:30%">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{e.name}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{e.name}" style="width:100%" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="department" style="width:20%">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{e.department}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{e.department}" style="width:100%"
                        label="department" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="salary" style="width:20%">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{e.salary}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{e.salary}" style="width:100%" label="salary" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="place" style="width:20%">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{e.place}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{e.place}" style="width:100%" label="place" />
                </f:facet>
            </p:cellEditor>
        </p:column>



        <p:column style="width:6%">
            <p:rowEditor />
        </p:column>

    </p:dataTable>

</h:form>

This is my managed bean:

@ManagedBean(name = "employees")
@ViewScoped
public class UserData  
{
    // properties
    private String name;
    private String department;
    private String salary;
    private String place;
    private ArrayList<Employee> eList;
    private Employee selectedemp;



    private void clear()
    {
        name="";
        department="";
        salary="";
        place="";
    }

//returning all employees   
    public ArrayList<Employee> geteList() throws ClassNotFoundException, SQLException {
        Connection conn=DbConnection.connectFunc();
        Statement stmt=conn.createStatement();
        String sql="select * from employee";
        ResultSet rs=stmt.executeQuery(sql);
        eList=new ArrayList<Employee>();
        while(rs.next())
        {
            Employee emp=new Employee();
            emp.setName(rs.getString(2));
            emp.setDepartment(rs.getString(3));
            emp.setSalary(rs.getString(4));
            emp.setPlace(rs.getString(5));
            eList.add(emp);
        }
        return eList;
    }
public void onEdit(RowEditEvent event) throws ClassNotFoundException, SQLException
    {

    Employee e=(Employee) event.getObject();
     name=e.getName();
        System.out.println(name);
        department=e.getDepartment();
        System.out.println(department);
        salary=e.getSalary();
        System.out.println(salary);
        place=e.getPlace();
        System.out.println(place);

        Connection conn=DbConnection.connectFunc();
        Statement stmt=conn.createStatement();
        String sql="update employee set department='"+department+"',salary='"+salary+"',place='"+place+"'where name='"+name+"'";
        int i=stmt.executeUpdate(sql);
        if(i!=0)
        {
            FacesContext context=FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("updated  successfully"));
        }
        conn.close();
    }
//onCancel
    public void onCancel(RowEditEvent event)
    {

    }
...setter and getters

If i use view scope, the following exception occurs:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.io.NotSerializableException: com.solv.datatable.UserData
    java.io.ObjectOutputStream.writeObject0(Unknown Source)
    java.io.ObjectOutputStream.writeObject(Unknown Source)
    java.util.HashMap.writeObject(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    java.io.ObjectOutputStream.writeObject0(Unknown Source)
    java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    java.io.ObjectOutputStream.writeObject0(Unknown Source)
    java.io.ObjectOutputStream.writeArray(Unknown Source)
    java.io.ObjectOutputStream.writeObject0(Unknown Source)
    java.io.ObjectOutputStream.writeObject(Unknown Source)
    java.util.HashMap.writeObject(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    java.io.ObjectOutputStream.writeObject0(Unknown Source)
    java.io.ObjectOutputStream.writeObject(Unknown Source)
    com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:325)
    com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:173)
    com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122)
    com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:166)
    com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:419)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.26 logs.

Answer

miroslav_mijajlovic picture miroslav_mijajlovic · Sep 19, 2013

Your bean must implement interface Serializable. That's what the error message says

The other problem is that you always return new List by getEList. You can load your ArayList in some kind of init method that will have annotation @PostConstruct.

@PostConstruct
public void init() {
       //code from getElist()...
}

And your getter shuld be classic getter that only returns eList. In that way you load your list when your view is created. In your case every update will load list again that is bad practice. Do not fetch data in getter. Editting is not working for you because you lose updated values by returning a new list every time.