Struts2中iterator标签遍历m…

来源:互联网 发布:崔万志淘宝店 编辑:程序博客网 时间:2024/05/17 23:37
原文地址:Struts2中iterator标签遍历map总结(转)作者:雪后DE阳光
1.MapAction.java
Java代码
  1. package com.zx.demo.action;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. importcom.opensymphony.xwork2.ActionSupport;  
  9. importcom.zx.demo.model.Product;  
  10. importcom.zx.demo.model.Student;  
  11.   
  12. public class MapActionextends ActionSupport  
  13. {   
  14.   
  15.     privateMap<String,String> map;  
  16.      
  17.     privateMap<String,Student> studentMap;  
  18.       
  19.     privateMap<String,String[]> arrayMap;  
  20.       
  21.     privateMap<String,List<Student>>listMap;   
  22.       
  23.   
  24.   
  25.     publicString testMap()  
  26.     {   
  27.         map=newHashMap<String,String>();  
  28.         map.put("1", "one");  
  29.         map.put("2", "two");  
  30.           
  31.         studentMap=newHashMap<String,Student>();  
  32.         studentMap.put("student1",newStudent(new Long(1),"20034140201","张三1","男",25));  
  33.         studentMap.put("student2",newStudent(new Long(2),"20034140202","张三2","女",26));  
  34.         studentMap.put("student3",newStudent(new Long(3),"20034140202","张三3","男",27));  
  35.           
  36.         arrayMap=newHashMap<String,String[]>();  
  37.         arrayMap.put("arr1", newString[]{"1","2003401","leejie","male","20"});  
  38.         arrayMap.put("arr2", newString[]{"2","2003402","huanglie","male","25"});  
  39.         arrayMap.put("arr3", newString[]{"3","2003403","lixiaoning","male","21"});  
  40.           
  41.           
  42.           
  43.         listMap=newHashMap<String,List<Student>>();  
  44.           
  45.         List<Student>list1=newArrayList<Student>();  
  46.         list1.add(newStudent(new Long(1),"20034140201","张三1","男",25));  
  47.         list1.add(newStudent(new Long(2),"20034140202","张三2","男",25));  
  48.         list1.add(newStudent(new Long(3),"20034140203","张三3","男",25));  
  49.         listMap.put("class1", list1);  
  50.           
  51.         List<Student>list2=newArrayList<Student>();  
  52.         list2.add(newStudent(new Long(1),"20034140301","李四1","男",20));  
  53.         list2.add(newStudent(new Long(2),"20034140302","李四2","男",21));  
  54.         list2.add(newStudent(new Long(3),"20034140303","李四3","男",22));  
  55.         list2.add(newStudent(new Long(4),"20034140304","李四4","男",23));  
  56.         listMap.put("class2", list2);  
  57.            
  58.           
  59.           
  60.           
  61.         returnSUCCESS;   
  62.           
  63.     }   
  64.   
  65.       
  66.       
  67.     publicMap<String, String> getMap() {  
  68.         returnmap;   
  69.     }   
  70.   
  71.     publicvoidsetMap(Map<String, String> map){   
  72.         this.map= map;   
  73.     }   
  74.       
  75.     publicMap<String, Student>getStudentMap() {   
  76.         returnstudentMap;   
  77.     }   
  78.   
  79.     publicvoidsetStudentMap(Map<String,Student> studentMap) {  
  80.         this.studentMap= studentMap;   
  81.     }   
  82.   
  83.   
  84.     publicMap<String, String[]>getArrayMap() {   
  85.         returnarrayMap;   
  86.     }   
  87.   
  88.   
  89.     publicvoidsetArrayMap(Map<String,String[]> arrayMap) {  
  90.         this.arrayMap= arrayMap;   
  91.     }   
  92.   
  93.   
  94.   
  95.     publicMap<String,List<Student>>getListMap() {   
  96.         returnlistMap;   
  97.     }   
  98.   
  99.   
  100.   
  101.     publicvoidsetListMap(Map<String,List<Student>>listMap) {   
  102.         this.listMap= listMap;   
  103.     }   
  104.       
  105.       
  106.  
package com.zx.demo.action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.opensymphony.xwork2.ActionSupport;import com.zx.demo.model.Product;import com.zx.demo.model.Student;public class MapAction extends ActionSupport{    private Map<String,String> map;    private Map<String,Student> studentMap;  private Map<String,String[]> arrayMap;  private Map<String,List<Student>> listMap;  public String testMap() {  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);           return SUCCESS;   }   public Map<String, String> getMap() {  return map; } public void setMap(Map<String, String> map) {  this.map = map; }  public Map<String, Student> getStudentMap() {  return studentMap; } public void setStudentMap(Map<String, Student> studentMap) {  this.studentMap = studentMap; } public Map<String, String[]> getArrayMap() {  return arrayMap; } public void setArrayMap(Map<String, String[]> arrayMap) {  this.arrayMap = arrayMap; } public Map<String, List<Student>> getListMap() {  return listMap; } public void setListMap(Map<String, List<Student>> listMap) {  this.listMap = listMap; }  }


2.testMap.jsp
Java代码 复制代码
  1. <%@ pagecontentType="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:propertyvalue="#column"/></td>  
  30.      <td><s:propertyvalue="value.id"/></td>  
  31.      <td><s:propertyvalue="value.num"/></td>  
  32.      <td><s:propertyvalue="value.name"/></td>  
  33.      <td><s:propertyvalue="value.sex"/></td>  
  34.      <td><s:propertyvalue="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:propertyvalue="#column"/></td>  
  54.      <td><s:propertyvalue="value[0]"/></td>  
  55.      <td><s:propertyvalue="value[1]"/></td>  
  56.      <td><s:propertyvalue="value[2]"/></td>  
  57.      <td><s:propertyvalue="value[3]"/></td>  
  58.      <td><s:propertyvalue="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"><tdrowspan="${total}"><s:propertyvalue="#column.key"/></td></s:if 
  79.         <td><s:propertyvalue="id"/></td>  
  80.         <td><s:propertyvalue="num"/></td>  
  81.         <td><s:propertyvalue="name"/></td>  
  82.         <td><s:propertyvalue="sex"/></td>  
  83.         <td><s:propertyvalue="age"/></td>  
  84.       </tr>  
  85.      </s:iterator>  
  86.   </s:iterator>  
  87. </table>  
  88.     
  89.     
  90. </body>  
  91. </html>  
0 0
原创粉丝点击