所以,我有这个网站结构
Page 1
Page 1.1
Page 2
Page 2.1
Page 2.1.1
Page 2.2
Page 3
Page 3.1
Page 3.2
Page 4
我希望使用递归函数构建<ul>
列表。我的功能看起来像这个
public String getMenu(Page rootPage, boolean base){
final Logger log = LoggerFactory.getLogger(this.getClass());
Iterator<Page> subPages = rootPage.listChildren();
StringBuilder output = new StringBuilder("<ul");
output.append(" id=\"drop-menu\"");
output.append(" class=\"popup-menu\">");
if(!base){
output.append("<li><a href=\"").append(rootPage.getPath()).append(".html\" class=\"showSubPage\" rel=\"").append(rootPage.getPath()).append("\">");
String title = rootPage.getPageTitle() == null ? rootPage.getTitle() : rootPage.getPageTitle();
output.append(title);
output.append("</a>");
output.append("</li>");
output.append("</ul>");
}
while(subPages.hasNext()){
output.append("<ul>");
log.info("som subpages here!");
Page curPage = subPages.next();
output.append("<li><a href=\"").append(curPage.getPath()).append(".html\" class=\"showSubPage\" rel=\"").append(curPage.getPath()).append("\">");
String title = curPage.getPageTitle() == null ? curPage.getTitle() : curPage.getPageTitle();
output.append(title);
output.append("</a>");
Iterator<Page> subSub = curPage.listChildren();
int tmpCtr = 0;
while(subSub.hasNext()){
tmpCtr++;
output.append(getMenu(subSub.next(), false));
}
output.append("</li>");
output.append("</ul>");
}
return output.toString();
}
,输出看起来像这样
<ul id="drop-menu" class="popup-menu">
<ul>
<li><a href="/menu-hier/afsafa.html" class="showSubPage" >Page 1</a>
<ul id="drop-menu" class="popup-menu">
<li><a href="/menu-hier/afsafa/sdgsdgdsf.html" class="showSubPage" rel="/menu-hier/afsafa/sdgsdgdsf">Page 1.1</a>
</li>
</ul>
</li>
</ul>
<ul>
<li><a href="/menu-hier/fdsafdsafas.html" class="showSubPage" rel="/menu-hier/fdsafdsafas">Page 2</a>
<ul id="drop-menu" class="popup-menu">
<li><a href="/menu-hier/fdsafdsafas/sdfgdsgsdg.html" class="showSubPage" rel="/menu-hier/fdsafdsafas/sdfgdsgsdg">Page 2.1</a>
</li>
</ul>
<ul>
<li><a href="/menu-hier/fdsafdsafas/sdfgdsgsdg/dsgsdgdsgfd.html" class="showSubPage" rel="/menu-hier/fdsafdsafas/sdfgdsgsdg/dsgsdgdsgfd">Page 2.1.1</a>
</li>
</ul>
<ul id="drop-menu" class="popup-menu">
<li><a href="/menu-hier/fdsafdsafas/fsdgdsfgsdgfs.html" class="showSubPage" rel="/menu-hier/fdsafdsafas/fsdgdsfgsdgfs">Page 2.2</a>
</li>
</ul>
</li>
</ul>
<ul>
<li><a href="/menu-hier/fgdsgfsdgdsfgsdg.html" class="showSubPage" rel="/menu-hier/fgdsgfsdgdsfgsdg">Page 3</a>
<ul id="drop-menu" class="popup-menu">
<li><a href="/menu-hier/fgdsgfsdgdsfgsdg/fsafsdafas.html" class="showSubPage" rel="/menu-hier/fgdsgfsdgdsfgsdg/fsafsdafas">Page 3.1</a>
</li>
</ul>
<ul id="drop-menu" class="popup-menu">
<li><a href="/menu-hier/fgdsgfsdgdsfgsdg/yxvcxyvvyxcv.html" class="showSubPage" rel="/menu-hier/fgdsgfsdgdsfgsdg/yxvcxyvvyxcv">Page 3.2</a>
</li>
</ul>
<ul id="drop-menu" class="popup-menu">
<li><a href="/menu-hier/fgdsgfsdgdsfgsdg/yxvcxyv.html" class="showSubPage" rel="/menu-hier/fgdsgfsdgdsfgsdg/yxvcxyv">Page 3.3</a>
</li>
</ul>
</li>
</ul>
<ul>
<li><a href="/menu-hier/dsfgsdfgdsfg.html" class="showSubPage" rel="/menu-hier/dsfgsdfgdsfg">Page 4</a>
</li>
</ul>
所以问题是,3级页面放置不正确。例如,页面2.1.1不在第2.1页下面。
感谢您的帮助!
答案 0 :(得分:1)
不确定,您希望HTML看起来如何,但是:
1)从你的代码中,<ul>
标签被插入两次子页面(一次在while循环中,然后在递归中再次调用getMenu())
2)我认为,您错过了<li>
代码下方的<ul>
代码。
3)你的代码看起来非常冗余和复杂,不能这么简单(未经测试):
public String getMenu(Page page, boolean isRoot) {
StringBuilder output = new StringBuilder();
if (isRoot) {
output.append("<ul id=\"drop-menu\"");
output.append(" class=\"popup-menu\">");
}
else {
output.append("<ul>");
}
output.append("<li><a href=\"")
.append(page.getPath())
.append(".html\" class=\"showSubPage\" rel=\"")
.append(rootPage.getPath()).append("\">");
String title = page.getPageTitle() == null ? page.getTitle() : page.getPageTitle();
output.append(title);
output.append("</a>");
Iterator<Page> subPages = page.listChildren();
while(subPages.hasNext()){
output.append(getMenu(subPages.next(), false));
}
output.append("</li>");
output.append("</ul>");
return output.toString();
}