zoukankan      html  css  js  c++  java
  • struts2_对Map进行双层迭代

    struts2的迭代是使用OGNL表达式的,要清楚的使用迭代,首先要了解OGNL表达式的原理,可以看OGNL表达式和struts2中的使用

    这里举个简单的例子,在action中进行Map<String, Map<String,String>>的封装,然后在jsp页面进行迭代,显示出信息:

    TestAction.java:

     1 public class TestAction extends ActionSupport {
     2     private Map map;
     3 
     4     public Map getMap() {
     5         return map;
     6     }
     7 
     8     public void setMap(Map map) {
     9         this.map = map;
    10     }
    11 
    12     @Override
    13     public String execute() throws Exception {
    14 
    15         Map<String, Map<String, String>> map = new TreeMap<String, Map<String, String>>();
    16         TreeMap<String, String> a = new TreeMap<String, String>();
    17         a.put("1", "1");
    18         a.put("2", "2");
    19         a.put("3", "3");
    20 
    21         TreeMap<String, String> b = new TreeMap<String, String>();
    22         b.put("4", "4");
    23         b.put("5", "5");
    24         b.put("6", "6");
    25 
    26         map.put("1", a);
    27         map.put("2", b);
    28 
    29         this.setMap(map);
    30 
    31         return SUCCESS;
    32     }
    33 }

    index.jsp:

     1     <body>
     2         <s:iterator value="map" status="s" id="id1">
     3             <s:property value="%{#id1.key}" />
     4             <br>
     5             <s:iterator value="#id1.value" status="ss" id="id2">
     6                 <s:property value="%{#id2.value}" />
     7             </s:iterator>
     8             <br>
     9         </s:iterator>
    10     </body>

    第2行是对Map<String, Map<String, String>>进行迭代,第5行是对Map<String,String>进行迭代.

    运行效果:

     

     

  • 相关阅读:
    自动化部署之jenkins及简介
    gitlab的备份与恢复与迁移
    P2561 [AHOI2002]黑白瓷砖
    P2042 [NOI2005]维护数列
    P2156 [SDOI2009]细胞探索
    P2154 [SDOI2009]虔诚的墓主人
    P2148 [SDOI2009]E&D
    2019.2.26考试T2 矩阵快速幂加速DP
    loj #6485. LJJ 学二项式定理 (模板qwq)
    P3224 [HNOI2012]永无乡
  • 原文地址:https://www.cnblogs.com/hanyuan/p/2652937.html
Copyright © 2011-2022 走看看