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;
}
}
这个action中给出了各种类型的map,下面我们有这样一个jsp,我们要将这些map展示到这个jsp中:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>struts2中的map遍历总结</title>
</head>
<body>
<b>1.map中的value为String字符串</b><br>
<s:iterator value="map" id="column">
<s:property value="#column"/><br>
key: <s:property value="key"/><br>
value:<s:property value="value"/><br>
******************************************<br>
</s:iterator>
<b>2.map中的value为Student对象</b>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
<td>key=value</td>
<td>ID</td>
<td>num</td>
<td>name</td>
<td>sex</td>
<td>age</td>
</tr>
<s:iterator value="studentMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="value.id"/></td>
<td><s:property value="value.num"/></td>
<td><s:property value="value.name"/></td>
<td><s:property value="value.sex"/></td>
<td><s:property value="value.age"/></td>
</tr>
</s:iterator>
</table>
<p>
<b>3.map中的value为String数组</b>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
<td>key=value</td>
<td>ID</td>
<td>num</td>
<td>name</td>
<td>sex</td>
<td>age</td>
</tr>
<s:iterator value="arrayMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="value[0]"/></td>
<td><s:property value="value[1]"/></td>
<td><s:property value="value[2]"/></td>
<td><s:property value="value[3]"/></td>
<td><s:property value="value[4]"/></td>
</tr>
</s:iterator>
</table>
<p>
<b>4.map中的value为list凑集</b>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
<td>class</td>
<td>ID</td>
<td>num</td>
<td>name</td>
<td>sex</td>
<td>age</td>
</tr>
<1.<s:iterator value="listHashMap" id="listid">
<s:iterator value="#listid.value" id="listidsub">
<tr>
<td><s:property value="key"/></td>
<td><s:property value="id"/></td>
<td><s:property value="num"/></td>
<td><s:property value="name"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</s:iterator>
</table>
</body>
</html>