PrimeFaces 3.0.M3 cell editor does not update value

I read there , but I cannot take the edited value from the datatable cellEditor, which gives me an unedited value. I am using jpa. Xhtml page:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.prime.com.tr/ui" template="/templates/masterLayout.xhtml"> <ui:define name="windowTitle"> learn </ui:define> <ui:define name="content"> <h:form> <p:dataTable value="#{lesson.lessonValue}" var="l" style="width: 400px"> <p:ajax event="rowEdit" listener="#{lesson.onEditRow}"/> <p:column headerText="Lessons" style="width: 300px"> <p:cellEditor> <f:facet name="output"> <h:outputText value="#{l.lessonName}"/> </f:facet> <f:facet name="input"> <p:inputText value="#{l.lessonName}" style="width: 100%"/> </f:facet> </p:cellEditor> </p:column> <p:column headerText="Options"> <p:rowEditor /> </p:column> </p:dataTable> </h:form> </ui:define> </ui:composition> 

lesson.java:

 public class lesson implements Serializable { private String name; protected EntityLesson[] lessonList; public String getName() { return name; } public void setName(String newValue) { name = newValue; } EntityManagerFactory emf = Persistence.createEntityManagerFactory("DefaultPU"); public EntityLesson[] getLessonValue() { EntityManager em = emf.createEntityManager(); List<EntityLesson> result; try { EntityTransaction entr = em.getTransaction(); boolean committed = false; entr.begin(); try { Query query = em.createQuery("SELECT l FROM EntityLesson l"); result = query.getResultList(); entr.commit(); committed = true; lessonList = new EntityLesson[result.size()]; lessonList = result.toArray(lessonList); } finally { if (!committed) entr.rollback(); } } finally { em.close(); } return lessonList; } public void onEditRow(RowEditEvent event) { EntityLesson editedLesson = (EntityLesson)event.getObject();//gives me unedited value ............................ ............................ } 

EntityLesson.java:

 @Entity @Table(name="lessonaaa") public class EntityLesson implements Serializable { @Id @Column(name="Lesson_Id", nullable=false) @GeneratedValue(strategy= GenerationType.IDENTITY) private int lessonId; @Column(name="Lessson", nullable=false, length=65) private String lessonName; public int getLessonId() { return lessonId; } public void setLessonId(int lessonId) { this.lessonId = lessonId; } public String getLessonName() { return lessonName; } public void setLesson (String lessonName) { this.lessonName = lessonName; } } 
+4
source share
4 answers

The problem is due to the JSF life cycle:

When your dataTable is displayed, it does JPQL to get a list of lessons. After that they are displayed. Now you are editing the entity and clicking "Save", the edited object in the list now has a new meaning. But what happens next is that the list is selected at a different time, and then the listener method is executed using the new loaded enitiy.

You can solve the problem if you save the list of objects in a local attribute in the bean view and fill it in the post post method (annotated with @PostContruct ), and you should make the bean @SessionScoped . Then use this list for datatable.

+8
source

My problem is similar:

"DataTable" contains as a "value" a list of objects:

 <p:dataTable id="category" var="category" value="#{categoriesBacking.categoriesListEdit}"> 

If I select one for editing, the object that is passed to the event contains a previously unmodified value. I noticed that this is because the dataTable value is a list. As a workflow (to use the component), I added "filterBy" to any of the columns. If the dataTable contains only one value, this value will be correctly interpreted by the passed event in the managed bean.

!!! The event object will be a modified instance.

I also use:

 <p:ajax event="rowEdit" update="@this" listener="#{categoriesBacking.onEditRow}" /> 

instead of dataTable 'rowEditListener'.

Again, this is just a workaround.

+1
source

I have almost exactly the same code, but instead of using the <p:ajax> , I use the rowEditListener dataTable attribute instead. Try instead:

 <p:dataTable ... rowEditListener="#{lesson.onEditRow}" ... > ... 
0
source

Such problems are usually related to your bean support. Your β€œlesson” class requires the @ManagedBean annotation (javax.faces.bean.ManagedBean). Just add

@ManagedBean (name = "YourBeanName")

@ViewScoped

just before the public class lesson implements Serializable { line public class lesson implements Serializable { in your lesson.java

0
source

All Articles