標籤:ace jsp struct ref color article request get 資料結構
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
jsp頁面使用el 按key擷取map中的對應值