【问题标题】:PrimeFaces 3.0.M3 Cell Editor does not update valuePrimeFaces 3.0.M3 单元格编辑器不更新值
【发布时间】:2011-10-09 23:50:37
【问题描述】:

我已阅读 there,但我无法从 primefaces 数据表 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> 

课程.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; }
    }

【问题讨论】:

标签: jsf-2 primefaces


【解决方案1】:

由于 JSF 生命周期而出现问题:

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

如果将实体列表存储在视图 bean 中的本地属性中并填充到 post 构造方法中(由 @PostContruct 注释),则可以解决该问题,并且您必须使视图 bean @SessionScoped。然后将此列表用于数据表。

【讨论】:

    【解决方案2】:

    我的问题类似:

    “dataTable”包含一个实体列表作为“值”:

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

    如果我选择一个进行编辑,则传递给事件的对象包含先前未修改的值。我观察到这是因为 dataTable 的值是一个列表。作为一种解决方法(为了能够使用该组件),我在任何“列”中添加了一个“filterBy”。如果 dataTable 将只包含一个值,则该值将被托管 bean 中传递的事件正确解释。

    !!!事件的对象将是修改后的实例。

    我也用:

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

    而不是 dataTable 的 'rowEditListener'。

    同样,这只是一种解决方法。

    【讨论】:

      【解决方案3】:

      除了使用 &lt;p:ajax&gt; 标签而不是使用 dataTable 的 rowEditListener 属性之外,我的代码几乎完全相同。试试这个:

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

      【讨论】:

      【解决方案4】:

      这类问题一般和你的backing bean有关。您的“课程”类需要 @ManagedBean (javax.faces.bean.ManagedBean) 注释。只需添加

      @ManagedBean (name="YourBeanName")

      @ViewScoped

      就在你的课程.java 中的public class lesson implements Serializable { 行之前

      【讨论】:

        猜你喜欢
        • 2011-09-15
        • 2013-05-16
        • 2014-08-03
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 2016-10-18
        • 2014-03-08
        相关资源
        最近更新 更多