• 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>进行迭代.

    运行效果:

     

     

  • 相关阅读:
    Python中的memoryview
    Python常见陷阱
    特殊方法 之 len __repr__ __str__
    collections模块
    使用math中的hypot实现向量
    Ellipsis对象
    array
    标准库heapq的使用
    Mysql常用命令
    使用npm查看安装的包
  • 原文地址:https://www.cnblogs.com/hanyuan/p/2652937.html
Copyright © 2020-2023  润新知