使用JSTL< c:forEach>迭代列表和地图的元素>标签 [英] Iterate over elements of List and Map using JSTL <c:forEach> tag

查看:124
本文介绍了使用JSTL< c:forEach>迭代列表和地图的元素>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如果任何人都可以给我一个简单的例子,说明如何在JSP中迭代java List我会大力赞赏。
Mark

解决方案

马克,这已经在您的上一个主题。但是好的,这里又是:

假设 $ {list} 指向一个列表< Object> ,然后如下

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

的功能基本上与普通Java中的相同:

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



$ b如果你有一个 List< Map< K,

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

的功能基本上与普通Java中的相同:


(Entry< K,V>条目:map.entrySet()){

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

$ b

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



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


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.

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

解决方案

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

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

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

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

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

<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":

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

The key and value are here not special methods or so. They are actually getter methods of Map.Entry object (click at the blue Map.Entry link 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 get prefix), all just according the Javabean specification.

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".

这篇关于使用JSTL&lt; c:forEach&gt;迭代列表和地图的元素&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆