我通过从HTML5
(使用JSP
循环)列表中读取数据,在ServletContext
中生成JSTL foreach
页面菜单项。这样我就会创建<nav> HTML5
标记的内容。
这很好用,我正在寻找我想要的页面。但是,当我查看HTML
页面源(从JSP生成)时,在foreach
循环中生成的空行(空格)太多。
这是生成此内容的代码:
<li><a href="#">Categories</a>
<ul class="droprightMenu">
<li><a href="#">All</a>
<ul class="droprightMenu">
<!-- first list iteration, top level categories, level1 -->
<c:forEach items="${categories }" var="catLevel1">
<c:if test="${catLevel1.getParentCategory()==null }">
<li><a href="#">${catLevel1.getName() }</a> <!-- second list iteration, level 2 categories,
parentCategory is level1 -->
<ul class="droprightMenu">
<c:forEach items="${categories }" var="catLevel2">
<c:if
test="${catLevel2.getParentCategory().getId()==catLevel1.getId() }">
<li><a href="#">${catLevel2.getName() }</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
<c:forEach items="${categories }" var="catLevel3">
<c:if
test="${catLevel3.getParentCategory().getId()==catLevel2.getId() }">
<li><a href="#">${catLevel3.getName() }</a></li>
</c:if>
</c:forEach>
</ul></li>
</c:if>
</c:forEach>
</ul></li>
</c:if>
</c:forEach>
</ul></li>
</ul></li>
,生成的内容如下所示:
<li><a href="#">Categories</a>
<ul class="droprightMenu">
<li><a href="#">All</a>
<ul class="droprightMenu">
<!-- first list iteration, top level categories, level1 -->
<li><a href="#">Sports</a> <!-- second list iteration, level 2 categories,
parentCategory is level1 -->
<ul class="droprightMenu">
<li><a href="#">Football</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
</ul></li>
<li><a href="#">Basketball</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
</ul></li>
<li><a href="#">Hockey</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
</ul></li>
</ul></li>
<li><a href="#">Politics</a> <!-- second list iteration, level 2 categories,
parentCategory is level1 -->
<ul class="droprightMenu">
</ul></li>
<li><a href="#">IT</a> <!-- second list iteration, level 2 categories,
parentCategory is level1 -->
<ul class="droprightMenu">
<li><a href="#">Software</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
</ul></li>
<li><a href="#">Hardware</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
</ul></li>
<li><a href="#">Programming</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
<li><a href="#">Java</a></li>
<li><a href="#">C#</a></li>
</ul></li>
<li><a href="#">Tutorials</a> <!-- third list iteration, level3 categories
parentCategory is level2 -->
<ul class="droprightMenu">
</ul></li>
</ul></li>
</ul></li>
</ul></li>
如何防止产生这些空行?因此页面源标记看起来很正常。