开发者

PrimeFaces 3.0.M3 Cell Editor does not update value

I have read there, but i can't take edited value from primefaces datatable cellEditor, it gives me 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; }
    }


The problem occurs because of the JSF lifecycle:

When your dataTable is displayed it executes the JPQL to retrieve the list of lessons. After that they are displayed. Now you edit on entity and hit save, the edited entity in the list has now the new value. But what happens next, is that the list is fetched another time and then the listener method is executed with the newly fetched enitiy.

You can solve the problem if you store the list of entities in a local attribute in the view bean and fill it in the post construct method (annotated by @PostContruct) and you have to make the view bean @SessionScoped. Then use this list for the datatable.


My problem is similar:

The 'dataTable' contains as 'value' a list of entities:

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

If I select one for edit, the object which is passed to the event contains the previously unmodified value. I observed that this is due to the fact that dataTable's value is a list. As a work-around (to be able to use the component) I added a 'filterBy' to any of the 'column'. If the dataTable will contain only one value, that value will be interpreted correctly by the passed event in the managed bean.

!!! The event's object will be the modified instance.

I also use:

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

instead of dataTable's 'rowEditListener'.

Again, this is just a workaround.


I have almost exactly the same code except instead of using the <p:ajax> tag I am instead using the rowEditListener attribute of dataTable. Try this instead:

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


This kind of problems are generally related to your backing bean. Your "lesson" class needs @ManagedBean (javax.faces.bean.ManagedBean) annotation. Simply add

@ManagedBean (name="YourBeanName")

@ViewScoped

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜