2011-10-09 36 views
4

我已阅读there,但我无法从primefaces数据表单编辑器中编辑值,它给了我未经编辑的值。我正在使用jpa。 XHTML页面:PrimeFaces 3.0.M3单元格编辑器不更新值

<?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; } 
    } 
+0

您不应该在getter中执行业务逻辑:http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062每当JSF调用数据表值的getter时需要渲染或处理**每一行**。 – BalusC

回答

8

问题:

当你的数据表上显示其执行JPQL检索的教训名单。之后,他们显示。 现在您在实体上编辑并点击保存,列表中已编辑的实体现在具有新值。 但接下来会发生什么,是再次提取列表,然后使用新提取的enitiy执行侦听器方法。

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

0

我几乎一模一样的代码只是不使用的<p:ajax>标签我代替使用dataTable的rowEditListener属性。试试这个:

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

rowEditListener参数在primefaces 3.x上不起作用(请参阅http://cagataycivici.wordpress.com/2011/05/13/ajax-events-as-behaviors/),但我在之前的primefaces 2.2.1上尝试过,但我无法达到更新的值 – Deniz

1

我的问题是相似的:

的“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'。

同样,这只是一个解决方法。是因为JSF生命周期的

0

这类问题通常与您的支持bean有关。您的“课程”类需要@ManagedBean(javax.faces.bean.ManagedBean)注释。只需添加

@ManagedBean(name = “YourBeanName”)

@ViewScoped

之前在课程public class lesson implements Serializable {线。java

相关问题