用户登录
用户注册

分享至

struts2标签 遍历map集合

  • 作者: 我被你的智商震惊的久久说不出话666
  • 来源: 51数据库
  • 2021-06-24
首先我们来构造几个map集合。

 

 假设如下代码 都是在ssh配置环境下搭建好,(至少struts2开发环境搭建好)

 

(1).java 代码  

 

     下面的student对象包含的字段为

 

     private long id;

     private string num;

     private string name;

     private string sex;

     private integer age;

      action中的代码

 

           private map<string,string> map; 

           private map<string,student> studentmap; 

           private map<string,string[]> arraymap; 

           private map<string,list<student>> listmap;   // 实现 四个map对象的get 和set方法。

 

 map=new hashmap<string,string>(); 

 map.put("1", "one"); 

 map.put("2", "two"); 

 

 studentmap=new hashmap<string,student>(); 

 studentmap.put("student1",new student(new long(1),"20034140201","张三1","男",25)); 

 studentmap.put("student2",new student(new long(2),"20034140202","张三2","女",26)); 

 studentmap.put("student3",new student(new long(3),"20034140202","张三3","男",27)); 

 

 arraymap=new hashmap<string,string[]>(); 

 arraymap.put("arr1", new string[]{"1","2003401","leejie","male","20"}); 

 arraymap.put("arr2", new string[]{"2","2003402","huanglie","male","25"}); 

 arraymap.put("arr3", new string[]{"3","2003403","lixiaoning","male","21"}); 

 

 

 

 listmap=new hashmap<string,list<student>>(); 

 

 list<student> list1=new arraylist<student>(); 

 list1.add(new student(new long(1),"20034140201","张三1","男",25)); 

 list1.add(new student(new long(2),"20034140202","张三2","男",25)); 

 list1.add(new student(new long(3),"20034140203","张三3","男",25)); 

 listmap.put("class1", list1); 

 

 list<student> list2=new arraylist<student>(); 

 list2.add(new student(new long(1),"20034140301","李四1","男",20)); 

 list2.add(new student(new long(2),"20034140302","李四2","男",21)); 

 list2.add(new student(new long(3),"20034140303","李四3","男",22)); 

 list2.add(new student(new long(4),"20034140304","李四4","男",23)); 

 listmap.put("class2", list2); 

 

(2).通过上述java代码我们已经构建好了4个map集合。 接下来的重头戏就是如何通过strut2的标签来获取map集合中的值。

 

<b>1.map中的value为string字符串</b><br>    

 

<s:iterator value="map" id="column">    

<s:property value="#column"/><br>      //这里获取到的值为key=value    即:键值对

key: <s:property value="key"/><br>      //这里的key为内置的,我们只要在value中写上key 即会有值

value:<s:property value="value"/><br>    //同样这里的value也为内置的

</s:iterator>    

 

 <b>2.map中的value为student对象</b>    

  <s:iterator value="studentmap" id="column">    

  <tr>    

   <td><s:property value="#column"/></td>    

   <td><s:property value="key"/></td>   

   <td><s:property value="value"/></td>   //这里的value返回的是一个student对象

   <td><s:property value="value.id"/></td>    //这里获取student对象中的属性值

   <td><s:property value="value.num"/></td>    

   <td><s:property value="value.name"/></td>    

   <td><s:property value="value.sex"/></td>    

   <td><s:property value="value.age"/></td>    

  </tr>    

  </s:iterator>    

 

遍历studentmap 还可以用下面方式,跟上面方式效果是一样的

 

<b>2.map中的value为student对象</b>    

  <s:iterator value="studentmap" id="column">    

  <tr>    

   <td><s:property value="#column"/></td>    

   <td><s:property value="key"/></td>   

   <s:iterator value="value">

   <td><s:property value="id"/></td>    

   <td><s:property value="num"/></td>    

   <td><s:property value="name"/></td>    

   <td><s:property value="sex"/></td>    

   <td><s:property value="age"/></td>    

    </s:iterator>

  </tr>    

  </s:iterator>  

 

 

  

  

  <b>3.map中的value为string数组</b>    

 <s:iterator value="arraymap" id="column">    

   <tr>    

    <td><s:property value="#column"/></td>  <!--同时取出键和值-->

    <td><s:property value="value[0]"/></td>    

    <td><s:property value="value[1]"/></td>    

    <td><s:property value="value[2]"/></td>    

    <td><s:property value="value[3]"/></td>    

    <td><s:property value="value[4]"/></td>    

   </tr>    

   </s:iterator>   

   

     <b>4.map中的value为list集合</b>    

      <s:iterator value="listmap" id="column">    

    <s:set name="total" value="#column.value.size"/>    //注意<s:set 标签的用法

    <s:iterator value="#column.value" status="s">     //这里#column.value 还是一个student的list集合,因而需要再次迭代一次

     <tr>    

     <s:property value="#s.first"/>     //判断是不是集合中的第一个对象

       <s:if test="#s.first"><td rowspan="${total}"><s:property value="#column.key"/></td></s:if>    

       <td><s:property value="id"/></td>    

       <td><s:property value="num"/></td>    

       <td><s:property value="name"/></td>    

       <td><s:property value="sex"/></td>    

       <td><s:property value="age"/></td>    

     </tr>    

    </s:iterator>    

 </s:iterator>    

 

 

 

软件
前端设计
程序设计
Java相关