zoukankan      html  css  js  c++  java
  • EL表达式取Map,List值的总结

    EL表达式取Map中的值:

    后台action 中:

    1. Map map = new HashMap();  
    2. map.put(key1,value1);  
    3. map.put(key2,value2);  
    4. map.put(key3,value3);  

    前台jsp 中使用EL获取Map:

    1 :  ${map[key1]}   ---- 直接取map中key=key1 的value;  例:  map .put("a","b"),  ${map["a"]}  就可以

    注意:如果key1 是数值,例如; 1

    后台 map.put(1, value1) , 前台 ${map[1]}将取不到值。原因:el表达式中数字1是Long类型,无法匹配后台map中的int。 修改 map.put(0L, value);  前台 :${map[1]}.

     

    1. <forEachitems="${map}" var="item">  
    2. <c:outvalue="${item.key}"/>  
    3. <c:outvalue="${item.value}"/>  
    4. </forEach>  


    EL 表达式取List的值:

    后台action :
    1. List list = new ArrayList();   
    2.          list.add("abc");  
    3.           list.add("123");  
    4.           list.add("haha");  
    5.           list.add("hehe");  
    前台jsp中:

    ${list[0]},         ${list[1]},          ${list[2]},          ${list[3]}

     

    练习例子:

     

    1. List<Map<String,Object>> students = new ArrayList<Map<String,Object>>();   
    2.         Map<String,Object> s1 = new HashMap<String,Object>();   
    3.         s1.put("name","jim");   
    4.         s1.put("age","15");   
    5.         students.add(s1);   
    6.            Map<String,Object> s2 = new HashMap<String,Object>();   
    7.         s2.put("name","lucy");   
    8.         s2.put("age","12");   
    9.         students.add(s2);   

     

    前台:

     

    1. <c:forEach var="student" items="$ {students}" >   
    2.           <tr>   
    3.           <span style="white-space:pre">    </span><td> <c:out value="$ {student.name}" default="wang"/> </td>   
    4.          <span style="white-space:pre"> </span><td> <c:out value="$ {student.age}" default="wang"/> </td>   
    5.           </tr>   
    6.  </c:forEach> 
  • 相关阅读:
    Mysql基本数据操作
    Mysql安装及自动化部署脚本方案
    CSS应用内容补充及小实例
    CSS详解
    python 装饰器
    HTML基础
    python 异常处理
    python生成器及迭代器
    python模块(二)
    python字符串格式化
  • 原文地址:https://www.cnblogs.com/jianming-chan/p/3452798.html
Copyright © 2011-2022 走看看