Struts2中iterator标签遍历map list总结

java] view plain copy print ?
  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.     private Map map; 
  11.     
  12.     private Map studentMap; 
  13.      
  14.     private Map arrayMap; 
  15.      
  16.     private Map> listMap; 
  17.      
  18.     public String testMap() 
  19.     { 
  20.         map=new HashMap(); 
  21.         map.put("1", "one"); 
  22.         map.put("2", "two"); 
  23.          
  24.         studentMap=new HashMap(); 
  25.         studentMap.put("student1",new Student(new Long(1),"20034140201","张三1","男",25)); 
  26.         studentMap.put("student2",new Student(new Long(2),"20034140202","张三2","女",26)); 
  27.         studentMap.put("student3",new Student(new Long(3),"20034140202","张三3","男",27)); 
  28.          
  29.         arrayMap=new HashMap(); 
  30.         arrayMap.put("arr1", new String[]{"1","2003401","leejie","male","20"}); 
  31.         arrayMap.put("arr2", new String[]{"2","2003402","huanglie","male","25"}); 
  32.         arrayMap.put("arr3", new String[]{"3","2003403","lixiaoning","male","21"}); 
  33.          
  34.          
  35.          
  36.         listMap=new HashMap>(); 
  37.          
  38.         List list1=new ArrayList(); 
  39.         list1.add(new Student(new Long(1),"20034140201","张三1","男",25)); 
  40.         list1.add(new Student(new Long(2),"20034140202","张三2","男",25)); 
  41.         list1.add(new Student(new Long(3),"20034140203","张三3","男",25)); 
  42.         listMap.put("class1", list1); 
  43.          
  44.         List list2=new ArrayList(); 
  45.         list2.add(new Student(new Long(1),"20034140301","李四1","男",20)); 
  46.         list2.add(new Student(new Long(2),"20034140302","李四2","男",21)); 
  47.         list2.add(new Student(new Long(3),"20034140303","李四3","男",22)); 
  48.         list2.add(new Student(new Long(4),"20034140304","李四4","男",23)); 
  49.         listMap.put("class2", list2); 
  50.           
  51.          
  52.          
  53.          
  54.         return SUCCESS; 
  55.          
  56.     } 
  57.      
  58.      
  59.     public Map getMap() { 
  60.         return map; 
  61.     } 
  62.     public void setMap(Map map) { 
  63.         this.map = map; 
  64.     } 
  65.      
  66.     public Map getStudentMap() { 
  67.         return studentMap; 
  68.     } 
  69.     public void setStudentMap(Map studentMap) { 
  70.         this.studentMap = studentMap; 
  71.     } 
  72.     public Map getArrayMap() { 
  73.         return arrayMap; 
  74.     } 
  75.     public void setArrayMap(Map arrayMap) { 
  76.         this.arrayMap = arrayMap; 
  77.     } 
  78.     public Map> getListMap() { 
  79.         return listMap; 
  80.     } 
  81.     public void setListMap(Map> listMap) { 
  82.         this.listMap = listMap; 
  83.     } 
  84.      
  85.      
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 map; private Map studentMap; private Map arrayMap; private Map> listMap; public String testMap() { map=new HashMap(); map.put("1", "one"); map.put("2", "two"); studentMap=new HashMap(); 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(); 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>(); List list1=new ArrayList(); 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 list2=new ArrayList(); 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 getMap() { return map; } public void setMap(Map map) { this.map = map; } public Map getStudentMap() { return studentMap; } public void setStudentMap(Map studentMap) { this.studentMap = studentMap; } public Map getArrayMap() { return arrayMap; } public void setArrayMap(Map arrayMap) { this.arrayMap = arrayMap; } public Map> getListMap() { return listMap; } public void setListMap(Map> listMap) { this.listMap = listMap; } }

2.testMap.jsp

[java] view plain copy print ?
  1. <%@ page contentType="text/html;charset=UTF-8" %> 
  2. <%@ taglib prefix="s" uri="/struts-tags" %> 
  3.  
  4.  
  5. struts2中的map遍历总结 
  6.  
  7.  
  8.    1.map中的value为String字符串
     
  9.    "map" id="column"
  10.    "#column"/>
     
  11.    key: "key"/>
     
  12.    value:"value"/>
     
  13.    ******************************************
     
  14.    
  15.   
  16.   
  17.   2.map中的value为Student对象 
  18.   "1" width="50%"  cellspacing="0" cellpadding="0"
  19.    
  20.  
  21.      
  22.  
  23.      
  24.  
  25.      
  26.  
  27.      
  28.  
  29.      
  30.  
  31.      
  32.  
  33.    
  34.  
  35.     "studentMap" id="column"
  36.    
  37.  
  38.     
  39.  
  40.     
  41.  
  42.     
  43.  
  44.     
  45.  
  46.     
  47.  
  48.     
  49.  
  50.    
  51.  
  52.      
  53.  
  54. key=value ID num name sex age
    "#column"/> "value.id"/> "value.num"/> "value.name"/> "value.sex"/> "value.age"/>
     
  55.  

     

  56.    
  57.    
  58.   3.map中的value为String数组 
  59.   "1" width="50%"  cellspacing="0" cellpadding="0"
  60.    
  61.  
  62.      
  63.  
  64.      
  65.  
  66.      
  67.  
  68.      
  69.  
  70.      
  71.  
  72.      
  73.  
  74.    
  75.  
  76.     "arrayMap" id="column"
  77.    
  78.  
  79.     
  80.  
  81.     
  82.  
  83.     
  84.  
  85.     
  86.  
  87.     
  88.  
  89.     
  90.  
  91.    
  92.  
  93.      
  94.  
  95. key=value ID num name sex age
    "#column"/> "value[0]"/> "value[1]"/> "value[2]"/> "value[3]"/> "value[4]"/>
     
  96.  

     

  97.   4.map中的value为list集合 
  98.   "1" width="50%"  cellspacing="0" cellpadding="0"
  99.    
  100.  
  101.      
  102.  
  103.      
  104.  
  105.      
  106.  
  107.      
  108.  
  109.      
  110.  
  111.      
  112.  
  113.    
  114.  
  115.      
  116.    "listMap" id="column"
  117.      "total" value="#column.value.size"/> 
  118.      "#column.value" status="s"
  119.      
  120.  
  121.         if test="#s.first">
  122. if
  123.        
  124.  
  125.        
  126.  
  127.        
  128.  
  129.        
  130.  
  131.        
  132.  
  133.      
  134.  
  135.       
  136.    
  137. class ID num name sex age
    "${total}">"#column.key"/> "id"/> "num"/> "name"/> "sex"/> "age"/>
     
  138.    
  139.    
  140.  
  141.  

你可能感兴趣的:(iterator,list,struts,string,class,border)