在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的命名約定。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<% 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: if test= "${item.product!=null || fn:length(item.product) != 0}" > <c:forEach items= "${item.product}" var= "product" > <c: if test= "${product.key=='userName'}" > ${product.value} </c: if > </c:forEach> </c: if > |
以上所述是小編給大家介紹的jstl EL表達式遍歷Map的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/qq_35624642/article/details/53761704