EL表达式遍历Map集合

时间:2022-11-22 20:51:57
<%
Map<String,String> map2 = new HashMap();
map2.put("a","hello world");
map2.put("b","this is map");
request.setAttribute("map2",map2);
%>

键值对遍历

<c:forEach var="item" items="${map2}">
${item.key} > ${item.value} <br>
</c:forEach>

键遍历

<c:forEach var="item" items="${map2}">
${item.key}<br>
</c:forEach>

值遍历

<c:forEach var="item" items="${map2}">
${item.value}<br>
</c:forEach>
<%
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
List<String> list2 = new ArrayList<String>();
list2.add("aaaaaa");
list2.add("bbbbbb");
Map<String,List<String>> map = new HashMap();
map.put("a",list);
map.put("b",list2);
request.setAttribute("map",map);
%>

通过键获得列表值,并遍历列表

<c:forEach var="item" items="${map['a']}">
${item }<br>
</c:forEach><br>
<c:forEach var="item" items="${map['b']}">
${item }<br>
</c:forEach>

map中值为列表,直接遍历列表中的每一项

<c:forEach var="item" items="${map}">
<c:forEach items="${item.value}" var="it">
${it }<br>
</c:forEach>
</c:forEach>

转自:http://www.cnblogs.com/cnjava/archive/2012/07/05/2578505.html