我需要循环遍历项目,但我也在寻找一种方法来获取项目列表中的下一个或上一个元素,以便如下所示,我可以检查第二个项目是否以第一个项目开头。有没有一种方法可以像列表或数组一样存储元素?
<c:forEach var="item" items="${entry.value.options}">
<c:set var="upper" value="${item.key }"/>
<c:choose>
<c:when test="${fn:startsWith(item, upper)}">
<c:if test="${item ne upper} }">
<ul>
<li>
<input class="parent" type="checkbox" name="criterias[${entry.key}]" value="${item.key}" />
<label>${item.value}</label>
</li>
</ul>
</c:if>
</c:when>
<c:otherwise>
<li>
<input class="child" type="checkbox" name="criterias[${entry.key}]" value="${item.key}" />
<label>${item.value}</label>
</li>
</c:otherwise>
</c:choose>
</c:forEach>
非常感谢!
最佳答案
您可以将类似 List<Item> list
的结构传递给 jsp其中 Item 有自己的 ArrayList,然后使用内部 forEach。它可能比扁平结构更容易。
<c:forEach var="item" items="${list}">
Access here item if needed <c:out value="${item.value}"/>
<c:forEach var="elem" items="${item}">
...
Access here elem <c:out value="${elem.value}"/>
...
</c:forEach>
</c:forEach>
关于foreach - JSTL ForEach 循环/数组或列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15192122/