jsp页面使用el 按key获取map中的对应值

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

原文地址:https://www.cnblogs.com/limeiky/p/6001436.html