EL表達式遍歷Map集合


 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM