使用 JSTL <c:forEach> 标签迭代 List 和 Map 的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2148658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:27:18  来源:igfitidea点击:

Iterate over elements of List and Map using JSTL <c:forEach> tag

listjspforeachhashmapjstl

提问by volvox

If I have a JSF backing bean return an object of type ArrayList, I should be able to use <c:foreach>to iterate over the elements in the list. Each element contains a map and although the question of how to access the map content through JSTL has been answered here, if I pass an array of such maps, I can't find how to iterate over them and still access the map content using JSTL. There's documentation which refers to simple iterators but not to those whose items are themselves maps.

如果我有一个 JSF 支持 bean 返回一个 ArrayList 类型的对象,我应该能够<c:foreach>用来迭代列表中的元素。每个元素都包含一个地图,虽然这里已经回答了如何通过 JSTL 访问地图内容的问题,但如果我传递了一个此类地图的数组,我无法找到如何迭代它们并仍然使用 JSTL 访问地图内容. 有文档指的是简单的迭代器,而不是那些项目本身就是地图的迭代器。

If anyone can give me a simple example of how a java List is iterated over in JSP I'd be massively appreciative. Mark

如果有人能给我一个简单的例子来说明如何在 JSP 中迭代 Java 列表,我将不胜感激。标记

回答by BalusC

Mark, this is already answered in your previous topic. But OK, here it is again:

马克,这已经在你之前的话题中得到了回答。但是好吧,又来了:

Suppose ${list}points to a List<Object>, then the following

假设${list}指向 a List<Object>,则以下

<c:forEach items="${list}" var="item">
    ${item}<br>
</c:forEach>

does basically the same as as following in "normal Java":

与“普通Java”中的以下内容基本相同:

for (Object item : list) {
    System.out.println(item);
}

If you have a List<Map<K, V>>instead, then the following

如果你有一个List<Map<K, V>>,那么以下

<c:forEach items="${list}" var="map">
    <c:forEach items="${map}" var="entry">
        ${entry.key}<br>
        ${entry.value}<br>
    </c:forEach>
</c:forEach>

does basically the same as as following in "normal Java":

与“普通Java”中的以下内容基本相同:

for (Map<K, V> map : list) {
    for (Entry<K, V> entry : map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
}

The keyand valueare here not special methods or so. They are actuallygetter methods of Map.Entryobject (click at the blue Map.Entrylink to see the API doc). In EL (Expression Language) you can use the .dot operator to access getter methods using "property name" (the getter method name without the getprefix), all just according the Javabean specification.

keyvalue在这里没有特殊的方法左右。它们实际上Map.Entry对象的getter 方法(单击蓝色Map.Entry链接以查看 API 文档)。在 EL(表达式语言)中,您可以使用.点运算符来访问使用“属性名称”(不带get前缀的 getter 方法名称)的 getter 方法,所有这些都只是根据 Javabean 规范。

That said, you really need to cleanup the "answers" in your previous topic as they adds noise to the question. Also read the comments I posted in your "answers".

也就是说,您确实需要清理上一个主题中的“答案”,因为它们会给问题增加噪音。另请阅读我在您的“答案”中发布的评论。

回答by Prateek RG

try this

尝试这个

<c:forEach items="${list}" var="map">
    <tr>
        <c:forEach items="${map}" var="entry">

            <td>${entry.value}</td>

        </c:forEach>
    </tr>
</c:forEach>