gpt4 book ai didi

java - 递归构建
    列表

转载 作者:行者123 更新时间:2023-12-01 13:47:48 24 4
gpt4 key购买 nike

所以,我有这个网站结构

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 部分下。

感谢您的帮助!

最佳答案

不确定您希望 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();
}

关于java - 递归构建 <ul> 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218350/

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