Line editing does not work in datatable primefaces

when editing data values, the values โ€‹โ€‹are not updated on the screen, as well as in the listening method. here i write a 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 โ€‹โ€‹are not updated. Pls give me a solution.

this is my xmhtml datatable

 <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 driven 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 "scope" after throwing an exception

 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. 
+3
source share
4 answers

Your bean should implement the Serializable interface. This error message reports

Another problem is that you always return a new List by getEList. You can load your ArayList in some init method that will contain the @PostConstruct annotation.

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

And your getter shuld will be a classic getter that returns an eList. This way you load your list when your view is created. In your case, each update will download the list again, which is bad practice. Do not retrieve data in getter. Editing does not work for you, because you lose updated values, each time returning a new list.

+3
source

IN

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

you are only updating messagess, not datatable. Try the following: update=":form:messages, :form:elist1" . It works for me

+1
source

I think you too:

  • You must define the rowKey attribute in the p: dataTable tag.
  • Or embed a datamodel (which you load into a datatable) that implements SelectableDataModel.

The rowKey attribute parameter will require a minimal amount of work. for example: <p:dataTable var="e" value="#{employees.eList}" id="elist1" rowKey="#{e.name}" editable="true">

where e.name is the identifier for each element in the datatable. It must also be a unique value for this object. (e.g. id)

0
source

It might help someone. I had a similar problem and tried my best on the Internet. In the end, I gave up, but later corrected it. The problem was that I had several h: form> used in my .xhtml, and the changes were not propagated for some reason. Hope this helps someone.

0
source

Source: https://habr.com/ru/post/954036/


All Articles