jsp頁面使用el 按key獲取map中的對應值
轉自:《jsp頁面使用el 按key獲取map中的對應值》地址:http://blog.csdn.net/baple/article/details/18517359
jsp頁面中的代碼:
<script type="text/javascript">
var msgTip = "${msgs['loginError']}";
alert(msgTip);
</script>
注意事項:
map名后不要加點,直接是[]
key要用單引號
在js中寫,要在整個取值外面加雙引號
補充:
10.10 使用JSTL標簽和EL表達式顯示HashMap中String的值
在Servlet文件中:
- // 通過EL表達式獲取HashMap簡單的內容
- HashMap hm_simple_string = new HashMap();
- hm_simple_string.put("key1", "value1");
- hm_simple_string.put("key2", "value2");
- request.setAttribute("hm_simple_string", hm_simple_string);
在JSP文件中:
- //通過EL表達式獲取HashMap簡單的內容:
- "${hm_simple_string['key1']}">
在JSP文件中,hm_simple_string是一個HashMap實例,Map是一個以key鍵value值配對的數據結構。通過key顯示出value,這樣就可以直接使用['key_name']的形式顯示出指定key所對應的value了。
顯示結果為:
通過EL表達式獲取HashMap簡單的內容value1
10.11 使用JSTL標簽和EL表達式顯示HashMap中bean屬性的值
在Servlet文件中:
- // 通過EL表達式取HashMap復雜的內容
- HashMap hm_complex = new HashMap();
- hm_complex.put("key1", Student_complex);
- request.setAttribute("hm_complex", hm_complex);
HashMap存放的是一個類class的實例。
在JSP文件中:
- //通過EL表達式取HashMap復雜的內容
- <c:out value="${hm_complex['key1']['username']}"></c:out>
hm_complex是一個HashMap類型,通過key可以取得value。但value是一個bean類型,所以就需要先通過['key1']來取得指定key所對應的value,再通過['username']來訪問bean的username屬性,並顯示出來。
顯示結果為:
通過EL表達式取HashMap復雜的內容:Student_complex