struts2 标签展示 map

在jsp页面用iterator 迭代时,如果 list中放的是map对象!而不是Object 用到的如下!

(平时用的list  中存放的都是Object对象)

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

你可能感兴趣的:(jsp,struts)