我正在处理数据库中的文件夹。每个文件夹都由其父文件引用:
id ref
root 0 0
me 1 0
folder 2 0
inme 3 1
单击其名称时会显示每个文件夹。 “commandLink”有一个参数:文件夹ID。它工作得很好,但我不知道如何保持路径的轨道:
我想要root > me > inme
。
有没有办法使用支持bean虚拟地在URL“... / root / me / inme”?
由于
答案 0 :(得分:0)
我自己写了,检查一下:
网页:强>
<h:form id="form1">
<h:outputText id="trackmsg" value="#{MyBean.trackMsg}"/>
<p:dataTable value="#{MyBean.folderList}" var="folder" style="width: 300px">
<p:column headerText="name">
<p:commandLink value="#{folder.name}" action="#{MyBean.track(folder.id)}" update=":form1:trackmsg"/>
</p:column>
<p:column headerText="id">
<h:outputText value="#{folder.id}"/>
</p:column>
<p:column headerText="ref">
<h:outputText value="#{folder.ref}"/>
</p:column>
</p:dataTable>
</h:form>
支持Bean:(只有两个关键方法:track()
和findParent()
)
private List<Folder> folderList=new ArrayList<>();
private String trackMsg="";//to show tracks of the path: root>me>inme
@PostConstruct
public void init(){//Just initialize thr list
folderList.add(new Folder("root", 0, 0));
folderList.add(new Folder("me", 1, 0));
folderList.add(new Folder("folder", 2, 0));
folderList.add(new Folder("inme", 3, 1));
folderList.add(new Folder("ininme", 4, 3));
}
public void track(int id){//to composite the track path
trackMsg="";
Folder selectedFolder=new Folder();
for(Folder fold:folderList) //find the selected Folder
if(fold.getId()==id)
{
selectedFolder=fold;
if(selectedFolder.getId()!=selectedFolder.getRef())//if so, it means the root folder
trackMsg=">"+selectedFolder.getName();
}
Folder parentFolder=findParent(selectedFolder);//get the parent folder of the selected folder
while(parentFolder!=null && parentFolder.getId()!=parentFolder.getRef()){//while there is parent folder and not root folder
trackMsg=">"+parentFolder.getName() + trackMsg;
parentFolder=findParent(parentFolder);//find the parent of parent
}
trackMsg="root"+trackMsg;//the last, root folder
}
public Folder findParent(Folder child){//find the parent folder of the passing folder
for(Folder fold:folderList)
if(fold.getId()==child.getRef())
return fold;
return null;
}
当我点击dataTable中的“ininme”时,它看起来像: