我想使用this primefaceExemple创建一个treeTable。
我正在循环进入对象以动态设置它。这是我的bean代码:
public class ModuleMB extends GenericMB<Module,ModuleService, Long> {
@Autowired
ModuleRepository moduleRepository;
private Document selectedDocument;
public ModuleMB() {
super(Module.class);
}
@Autowired
@Override
public void setService(ModuleService service) {
this.service = service;
}
public TreeNode getRoot() {
TreeNode root = new DefaultTreeNode("root", null);
Iterable<Module> moduleList = moduleRepository.findAll();
for (Module m : moduleList) {
TreeNode modules = new DefaultTreeNode(new Document(m.getLabel(), Long.toString(m.getLastModified()), m.getClass().getName()), root);
}
for (TreeNode curModuleNode : root.getChildren()) {
System.out.println("ROWKEY : " + curModuleNode.getRowKey());
for (Module m : moduleList) {
System.out.println("MODULE LABEL : " + m.getLabel());
for (ModuleVersion mv : m.getVersions()) {
System.out.println("MV LABEL : " + mv.getLabel());
TreeNode moduleVersions = new DefaultTreeNode(new Document(mv.getLabel(), Long.toString(mv.getLastModified()), mv.getClass().getName()), curModuleNode);
}
}
}
return root;
}
public Document getSelectedDocument() {
return selectedDocument;
}
public void setSelectedDocument(Document selectedDocument) {
this.selectedDocument = selectedDocument;
}
}
这是我的index.xhtml模板:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:sec="http://www.springframework.org/security/tags">
<h:body>
<ui:composition template="/pages/theme/back/backOfficeLayout.xhtml">
<ui:define name="bo-header">
<p class="lead blog-description">Module administration.</p>
</ui:define>
<ui:define name="content">
<h:form id="form">
<h:button outcome="add" id="addModule" value="Create new module" styleClass="btn btn-primary" /><br /><br />
<p:treeTable id="treetable" value="#{moduleMB.root}" var="document">
<f:facet name="header">Document Viewer</f:facet>
<p:column style="width:32%">
<f:facet name="header">Name</f:facet>
<h:outputText value="#{document.name}" />
</p:column>
<p:column style="width:32%">
<f:facet name="header">Update date</f:facet>
<h:outputText value="#{document.size}" />
</p:column>
<p:column style="width:32%">
<f:facet name="header">Type</f:facet>
<h:outputText value="#{document.type}" />
</p:column>
<p:column style="width:4%">
<p:commandLink update=":form:documentPanel" oncomplete="PF('documentDialog').show()" title="View Detail" styleClass="ui-icon ui-icon-search">
<f:setPropertyActionListener value="#{document}"
target="#{moduleMB.selectedDocument}" />
</p:commandLink>
</p:column>
</p:treeTable>
<p:dialog id="dialog" header="Document Detail" showEffect="fade" widgetVar="documentDialog" modal="true">
<p:outputPanel id="documentPanel">
<h:panelGrid columns="2" cellpadding="5" rendered="#{not empty moduleMB.selectedDocument}">
<h:outputLabel for="name" value="Name: " />
<h:outputText id="name" value="#{moduleMB.selectedDocument.name}" style="font-weight:bold" />
<h:outputLabel for="size" value="Size: " />
<h:outputText id="size" value="#{moduleMB.selectedDocument.size}" style="font-weight:bold" />
<h:outputLabel for="type" value="Type " />
<h:outputText id="type" value="#{moduleMB.selectedDocument.type}" style="font-weight:bold" />
</h:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
查看结果如下:
这里是控制台输出(我没有把所有,因为很多重复):
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
...
我不明白为什么我的模块版本附加到每个模块,因为只有第一个模块获得模块版本,而不是其他两个模块!
另外,当点击右边的loopt时,单击moduleVersion循环时会写入模块信息而不是模块版本信息:
感谢阅读,
施奈特
答案 0 :(得分:0)
由于前两个嵌套的for循环,您将所有Module的ModuleVersions添加到每个模块。我将最内层的for循环(for(ModuleVersion mv:
)移动到原始的for循环中:
public TreeNode getRoot() {
TreeNode root = new DefaultTreeNode("root", null);
Iterable<Module> moduleList = moduleRepository.findAll();
for (Module m : moduleList) {
TreeNode moduleNode = new DefaultTreeNode(new Document(m.getLabel(), Long.toString(m.getLastModified()), m.getClass().getName()), root);
System.out.println("ROWKEY : " + moduleNode.getRowKey());
System.out.println("MODULE LABEL : " + m.getLabel());
for (ModuleVersion mv : m.getVersions()) {
System.out.println("MV LABEL : " + mv.getLabel());
TreeNode moduleVersions = new DefaultTreeNode(new Document(mv.getLabel(), Long.toString(mv.getLastModified()), mv.getClass().getName()), curModuleNode);
}
}
return root;
}