我需要在嵌套循环中访问JSTL中的两个依赖ArrayList。首先,我迭代一个字符串类型ArrayList,然后循环中的值将用于访问另一个ArrayList。
<c:forEach items="${productCatagoryList}" var="category">
<c:forEach items=${${category}} var="item">
${item.productName}
</c:forEach>
</c:forEach>在这里,从第一个foreach循环中,我将获得作为字符串值的类别,对于所有这些类别,还有另一个包含一些产品的ArrayList。
因此,对于来自第一个循环的每个字符串值,将用于第二个foreach循环。
代码的第二行会导致错误。如何在第二个循环ans项中使用第一个循环的结果?
发布于 2016-12-21 23:30:27
可以使用参数category在同一个类中创建方法。应该是字符串类型。然后您可以从EL调用此方法。较新的EL允许调用方法,您可以使用这些方法而不是自定义函数。
<c:forEach items="${productCategoryList}" var="category">
<c:forEach items=${getProductsForCategory(category)} var="item">
${item.productName}
</c:forEach>
</c:forEach>如果您将类别映射为这样的产品
Map<String, List<Product>> productCategory;然后,您可以简单地为这个变量使用getter。
<c:forEach items="${productCategoryList}" var="category">
<c:forEach items=${productCategory[category]} var="item">
${item.productName}
</c:forEach>
</c:forEach>https://stackoverflow.com/questions/41250706
复制相似问题