gpt4 book ai didi

jsf-2 - PrimeFaces 3.0.M3 单元编辑器不更新值

转载 作者:行者123 更新时间:2023-12-03 01:37:25 25 4
gpt4 key购买 nike

我已阅读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>

类(class).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 生命周期而出现问题:

当您的数据表显示时,它会执行 JPQL 来检索类(class)列表。之后它们就会显示出来。现在您编辑实体并点击保存,列表中编辑的实体现在具有新值。但接下来会发生的是,再次获取列表,然后使用新获取的实体执行监听器方法。

如果将实体列表存储在 View bean 的本地属性中并将其填充到 post 构造方法(由 @PostContruct 注释)中,并且必须使查看 bean @SessionScoped。然后将此列表用于数据表。

关于jsf-2 - PrimeFaces 3.0.M3 单元编辑器不更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7707377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com