jsp頁面使用el 按key擷取map中的對應值

來源:互聯網
上載者:User

標籤: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檔案中:

  1. // 通過EL運算式擷取HashMap簡單的內容  
  2. HashMap hm_simple_string = new HashMap();  
  3. hm_simple_string.put("key1", "value1");  
  4. hm_simple_string.put("key2", "value2");  
  5. request.setAttribute("hm_simple_string", hm_simple_string); 

在JSP檔案中:

  1. //通過EL運算式擷取HashMap簡單的內容:  
  2. "${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檔案中:

  1. // 通過EL運算式取HashMap複雜的內容  
  2. HashMap hm_complex = new HashMap();  
  3. hm_complex.put("key1", Student_complex);  
  4. request.setAttribute("hm_complex", hm_complex); 

HashMap存放的是一個類class的執行個體。

在JSP檔案中:

  1. //通過EL運算式取HashMap複雜的內容  
  2. <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中的對應值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.