EL遍歷集合


在EL中,方括號運算符用來檢索數組和集合的元素。對於實現 java.util.Map 接口的集合,方括號運算符使用關聯的鍵查找存儲在映射中的值。

在方括號中指定鍵,並將相應的值作為表達式的值返回。例如,表達式 ${map['key']} 返回與 map標識符所引用的 Map 中的 "key" 鍵相關聯的值。 
當forEach 的items屬性中的表達式的值是java.util.Map時,則var中命名的變量的類型就是 java.util.Map.Entry。這時var=entry的話,用表達式${entry.key}取得鍵名。 用表達${entry.value}得到每個entry的值。這是因為java.util.Map.Entry對象有getKey和getValue方 法,表達式語言遵守JavaBean的命名約定。

 Java代碼  

  1. <%   
  2. Map<String,String> map2 = new HashMap();   
  3. map2.put("a","hello world");   
  4. map2.put("b","this is map");   
  5. request.setAttribute("map2",map2);   
  6. %>   

  鍵值對遍歷

 Java代碼  

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

 

 鍵遍歷

 Java代碼  

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

 

 值遍歷

Java代碼  
  1. <c:forEach var="item" items="${map2}">   
  2. ${item.value}<br>   
  3. </c:forEach>   
  4. <body>   

 

Java代碼  
  1. <%   
  2. List<String> list = new ArrayList<String>();   
  3. list.add("first");   
  4. list.add("second");   
  5. List<String> list2 = new ArrayList<String>();   
  6. list2.add("aaaaaa");   
  7. list2.add("bbbbbb");   
  8. Map<String,List<String>> map = new HashMap();   
  9. map.put("a",list);   
  10. map.put("b",list2);   
  11. request.setAttribute("map",map);   
  12. %>   

 

通過鍵獲得列表值,並遍歷列表

 

Java代碼  
  1. <c:forEach var="item" items="${map['a']}">   
  2. ${item }<br>   
  3. </c:forEach><br>   
  4. <c:forEach var="item" items="${map['b']}">   
  5. ${item }<br>   
  6. </c:forEach>  

 map中值為列表,直接遍歷列表中的每一項

 

Java代碼  
  1. <c:forEach var="item" items="${map}">   
  2. <c:forEach items="${item.value}" var="it">   
  3. ${it }<br>   
  4. </c:forEach>   
  5. </c:forEach>  

 ----------------------------------------------------------------------------------------------

1、java map的便利取值

Java代碼  
  1. Map<String,String> map = new HashMap<String,String>();  
  2. map.put("key1", "value1");  
  3. map.put("key2", "value2");  
  4.   
  5. Iterator<Entry<String,String>> iter = map.entrySet().iterator();  
  6. while(iter.hasNext()) {  
  7.     Entry<String, String> entry = iter.next();  
  8.     System.out.println(entry.getKey()+"," + entry.getValue());  
  9. }  
  10.   
  11. Set<Map.Entry<String, String>> entry  = map.entrySet();  
  12. for(Map.Entry<String, String> e : entry) {  
  13.     System.out.println(e.getKey()+"," + e.getValue());  
  14. }  
  15.           

 2、jstl的迭代取值
     當forEach 的items屬性中的表達式的值是java.util.Map時,則var中命名的變量的類型就是 java.util.Map.Entry。這時var=entry的話,用表達式${entry.key}取得鍵名。 

  用表達式${entry.value}得到每個entry的值。這是因為java.util.Map.Entry對象有getKey和getValue方法,表達式語言遵守JavaBean的命名約定

Java代碼  
  1. <c:forEach items="${map}" var="entry">    
  2.    <c:out value="${entry.key}" />    
  3.    <c:out value="${entry.value}" />    
  4. </c:forEach>   

 3、jstl根據key取值

 

一、如果知道key值,${map.key1}

二、如果key值是個變量, 則${map[key]}, 使用數組訪問方式,同樣也可以用在知道key   ${map["key1"]}


免責聲明!

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



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