在我的java web项目中,我想测试如何在dataTable中显示列表,我有一个示例,其中有显示。我尝试按照示例创建一个新的xhtml页面,其中我复制了示例的dataTable,我添加了一个新的dataTable,并创建了一个名为StatPanne的新bean。示例的bean是NaturePanneController。 问题是新的dataTable是空的。我明白新bean返回的列表是空的,但我不知道为什么,请你帮我。谢谢。如何在dataTable中显示来自数据库的数据?
@Name("naturePanneController")
@Scope(ScopeType.CONVERSATION)
public class NaturePanneController {
@In
EntityManager entityManager;
@In
@Out
NaturePanne naturePanne;
private Boolean edit = false;
private Integer paramid;
public Boolean getEdit() {
return edit;
}
public Integer getParamid() {
return paramid;
}
public void setParamid(Integer paramid) {
this.paramid = paramid;
}
public void setEdit(Boolean edit) {
this.edit = edit;
}
public List<NaturePanne> getNaturePanne() {
return entityManager.createQuery("select a from NaturePanne a")
.getResultList();
}
public void selectNP(NaturePanne np) {
naturePanne = np;
}
public void supprimer() {
if (naturePanne != null) {
//if (nn != null) {
Integer a = getParamid();
NaturePanne np = entityManager.find(NaturePanne.class, a);
entityManager.remove(np);
}
}
public void ajouter(ActionEvent e) {
entityManager.persist(naturePanne);
naturePanne = new NaturePanne();
}
public void mettreajour() {
if (getEdit() == true) {
setEdit(false);
} else {
setEdit(true);
}}}
我的新豆StatPanne.java
@Name("statPanne")
@Scope(ScopeType.CONVERSATION)
public class StatPanne {
@In
EntityManager entityManager;
@In
@Out
NaturePanne naturePanne;
private Boolean edit = false;
private Integer paramid;
public Boolean getEdit() {
return edit;
}
public Integer getParamid() {
return paramid;
}
public void setParamid(Integer paramid) {
this.paramid = paramid;
}
public void setEdit(Boolean edit) {
this.edit = edit;
}
public List<NaturePanne> getNaturePanne() {
return entityManager.createQuery("select a from NaturePanne a").getResultList();
}
public void selectNP(NaturePanne np) {
naturePanne = np;
}
public void supprimer() {
if (naturePanne != null) {
//if (nn != null) {
Integer a = getParamid();
NaturePanne np = entityManager.find(NaturePanne.class, a);
entityManager.remove(np);
}
}
public void ajouter(ActionEvent e) {
entityManager.persist(naturePanne);
naturePanne = new NaturePanne();
}
public void mettreajour() {
if (getEdit() == true) {
setEdit(false);
} else {
setEdit(true);}}}
我的页面XHTML其中包含了两个DataTable
<!-- the new dataTable: empty table -->
<h:form>
<rich:dataTable value="#{statPanne.naturePanne}" var="lp">
<h:column headerClass="headerleftfacet">
<f:facet name="header">
<h:outputText value="#{msg.nom_panne}" />
</f:facet>
<h:outputText id="outtt" value="#{lp.lib_naturepanne}"/>
</h:column>
</rich:dataTable>
</h:form>
<!-- the old dataTable:works well -->
<h:form>
<rich:dataTable var="panne" value="#{naturePanneController.naturePanne}">
<h:column headerClass="headerleftfacet">
<f:facet name="header">
<h:outputText value="#{msg.nom_panne}" />
</f:facet>
<h:outputText value="#{panne.lib_naturepanne}"/>
</h:column>
</rich:dataTable>
</h:form>
实体NaturePanne.java
@Entity
@Name("naturePanne")
@Scope(ScopeType.EVENT)
@AutoCreate
@Table(name = "naturepanne")
public class NaturePanne implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id_naturepanne;
private String lib_naturepanne;
//Getters/Setters
如果从数据库返回的列表是空的,它是纯数据库/ jpa问题,而不是java-se jsf或datatable – Kukeltje
感谢您的回复,您有任何建议 – marwa
@Kukeltje在您看来,为什么我的新dataTable是空的,尽管我复制了同一个bean? – marwa