<% 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