- 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;
- }
-
-
- }
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
- <%@ page contentType="text/html;charset=UTF-8" %>
- <%@ taglib prefix="s" uri="/struts-tags" %>
-
-
- struts2中的map遍历总结
-
-
- 1.map中的value为String字符串
- "map" id="column">
- "#column"/>
- key: "key"/>
- value:"value"/>
- ******************************************
-
-
-
- 2.map中的value为Student对象
-
"1" width="50%" cellspacing="0" cellpadding="0">
-
-
key=value |
-
ID |
-
num |
-
name |
-
sex |
-
age |
-
- "studentMap" id="column">
-
-
"#column"/> |
-
"value.id"/> |
-
"value.num"/> |
-
"value.name"/> |
-
"value.sex"/> |
-
"value.age"/> |
-
-
-
-
-
-
- 3.map中的value为String数组
-
"1" width="50%" cellspacing="0" cellpadding="0">
-
-
key=value |
-
ID |
-
num |
-
name |
-
sex |
-
age |
-
- "arrayMap" id="column">
-
-
"#column"/> |
-
"value[0]"/> |
-
"value[1]"/> |
-
"value[2]"/> |
-
"value[3]"/> |
-
"value[4]"/> |
-
-
-
-
- 4.map中的value为list集合
-
"1" width="50%" cellspacing="0" cellpadding="0">
-
-
class |
-
ID |
-
num |
-
name |
-
sex |
-
age |
-
-
- "listMap" id="column">
- "total" value="#column.value.size"/>
- "#column.value" status="s">
-
- if test="#s.first">
"${total}">"#column.key"/> | if>
-
"id"/> |
-
"num"/> |
-
"name"/> |
-
"sex"/> |
-
"age"/> |
-
-
-
-
-
-
-