Struts2中iterator标签遍历map list总结

 1  package com.zx.demo.action;
 2 import java.util.ArrayList;
 3 import java.util.HashMap;
 4 import java.util.List;
 5 import java.util.Map;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.zx.demo.model.Product;
 8 import com.zx.demo.model.Student;
 9 public class MapAction extends ActionSupport
10 {
11     private Map<String,String> map;
12    
13     private Map<String,Student> studentMap;
14     
15     private Map<String,String[]> arrayMap;
16     
17     private Map<String,List<Student>> listMap;
18     
19     public String testMap()
20     {
21         map=new HashMap<String,String>();
22         map.put("1", "one");
23         map.put("2", "two");
24         
25         studentMap=new HashMap<String,Student>();
26         studentMap.put("student1",new Student(new Long(1),"20034140201","张三1","男",25));
27         studentMap.put("student2",new Student(new Long(2),"20034140202","张三2","女",26));
28         studentMap.put("student3",new Student(new Long(3),"20034140202","张三3","男",27));
29         
30         arrayMap=new HashMap<String,String[]>();
31         arrayMap.put("arr1", new String[]{"1","2003401","leejie","male","20"});
32         arrayMap.put("arr2", new String[]{"2","2003402","huanglie","male","25"});
33         arrayMap.put("arr3", new String[]{"3","2003403","lixiaoning","male","21"});
34         
35         
36         
37         listMap=new HashMap<String,List<Student>>();
38         
39         List<Student> list1=new ArrayList<Student>();
40         list1.add(new Student(new Long(1),"20034140201","张三1","男",25));
41         list1.add(new Student(new Long(2),"20034140202","张三2","男",25));
42         list1.add(new Student(new Long(3),"20034140203","张三3","男",25));
43         listMap.put("class1", list1);
44         
45         List<Student> list2=new ArrayList<Student>();
46         list2.add(new Student(new Long(1),"20034140301","李四1","男",20));
47         list2.add(new Student(new Long(2),"20034140302","李四2","男",21));
48         list2.add(new Student(new Long(3),"20034140303","李四3","男",22));
49         list2.add(new Student(new Long(4),"20034140304","李四4","男",23));
50         listMap.put("class2", list2);
51          
52         
53         
54         
55         return SUCCESS;
56         
57     }
58     
59     
60     public Map<String, String> getMap() {
61         return map;
62     }
63     public void setMap(Map<String, String> map) {
64         this.map = map;
65     }
66     
67     public Map<String, Student> getStudentMap() {
68         return studentMap;
69     }
70     public void setStudentMap(Map<String, Student> studentMap) {
71         this.studentMap = studentMap;
72     }
73     public Map<String, String[]> getArrayMap() {
74         return arrayMap;
75     }
76     public void setArrayMap(Map<String, String[]> arrayMap) {
77         this.arrayMap = arrayMap;
78     }
79     public Map<String, List<Student>> getListMap() {
80         return listMap;
81     }
82     public void setListMap(Map<String, List<Student>> listMap) {
83         this.listMap = listMap;
84     }
85     
86     
87 }
 1  <%@ page contentType="text/html;charset=UTF-8" %>
 2 <%@ taglib prefix="s" uri="/struts-tags" %>
 3 <html>
 4 <head>
 5 <title>struts2中的map遍历总结</title>
 6 </head>
 7 <body>
 8    <b>1.map中的value为String字符串</b><br>
 9    <s:iterator value="map" id="column">
10    <s:property value="#column"/><br>
11    key: <s:property value="key"/><br>
12    value:<s:property value="value"/><br>
13    ******************************************<br>
14   </s:iterator>
15  
16  
17   <b>2.map中的value为Student对象</b>
18   <table border="1" width="50%"  cellspacing="0" cellpadding="0">
19     <tr>
20       <td>key=value</td>
21       <td>ID</td>
22       <td>num</td>
23       <td>name</td>
24       <td>sex</td>
25       <td>age</td>
26     </tr>
27     <s:iterator value="studentMap" id="column">
28     <tr>
29      <td><s:property value="#column"/></td>
30      <td><s:property value="value.id"/></td>
31      <td><s:property value="value.num"/></td>
32      <td><s:property value="value.name"/></td>
33      <td><s:property value="value.sex"/></td>
34      <td><s:property value="value.age"/></td>
35     </tr>
36     </s:iterator>
37   </table>
38   <p>
39   
40   
41   <b>3.map中的value为String数组</b>
42   <table border="1" width="50%"  cellspacing="0" cellpadding="0">
43     <tr>
44       <td>key=value</td>
45       <td>ID</td>
46       <td>num</td>
47       <td>name</td>
48       <td>sex</td>
49       <td>age</td>
50     </tr>
51     <s:iterator value="arrayMap" id="column">
52     <tr>
53      <td><s:property value="#column"/></td>
54      <td><s:property value="value[0]"/></td>
55      <td><s:property value="value[1]"/></td>
56      <td><s:property value="value[2]"/></td>
57      <td><s:property value="value[3]"/></td>
58      <td><s:property value="value[4]"/></td>
59     </tr>
60     </s:iterator>
61   </table>
62   <p>
63   <b>4.map中的value为list集合</b>
64   <table border="1" width="50%"  cellspacing="0" cellpadding="0">
65     <tr>
66       <td>class</td>
67       <td>ID</td>
68       <td>num</td>
69       <td>name</td>
70       <td>sex</td>
71       <td>age</td>
72     </tr>
73     
74    <s:iterator value="listMap" id="column">
75      <s:set name="total" value="#column.value.size"/>
76      <s:iterator value="#column.value" status="s">
77       <tr>
78         <s:if test="#s.first"><td rowspan="${total}"><s:property value="#column.key"/></td></s:if>
79         <td><s:property value="id"/></td>
80         <td><s:property value="num"/></td>
81         <td><s:property value="name"/></td>
82         <td><s:property value="sex"/></td>
83         <td><s:property value="age"/></td>
84       </tr>
85      </s:iterator>
86   </s:iterator>
87  </table>
88   
89   
90 </body>
91 </html>

 

posted @ 2013-03-29 14:00  一筐  阅读(1115)  评论(0编辑  收藏  举报