PrimeFaces 3.0.M3单元格编辑器不更新值

Den*_*niz 4 primefaces jsf-2

我在那里读过,但我不能从primefaces datatable cellEditor获取编辑值,它给了我未经编辑的值.我正在使用jpa.xhtml页面:

<?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> 
Run Code Online (Sandbox Code Playgroud)

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
        ............................
        ............................
    }
Run Code Online (Sandbox Code Playgroud)

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; }
    }
Run Code Online (Sandbox Code Playgroud)

McI*_*osh 8

出现问题的原因是JSF生命周期:

显示dataTable时,它会执行JPQL以检索课程列表.之后显示它们.现在您在实体上编辑并点击保存,列表中已编辑的实体现在具有新值.但接下来会发生的是,列表是另一次获取的,然后使用新获取的enitiy执行侦听器方法.

如果将实体列表存储在视图bean的本地属性中并将其填充到post构造方法(注释@PostContruct)中,则必须创建视图bean,才能解决问题@SessionScoped.然后将此列表用于数据表.