在action 中定义一个List<Map<String,Object>>集合 在jsp 怎样遍历其中信息

在action 中定义一个List<Map<String,Object>>集合 在jsp 怎样遍历其中信息,第1张

是这样遍历的:
<s:iterater value = "list名字">
<s:property value="username"> //直接把放在list里对象的属性名写出来就行了
</s:iterater>

<c:forEach var="aa" items="a"><!-- a是你session传过来的集合 -->
<c:out value="${aaid}"></c:out>
<c:out value="${aaname}"></c:out>
</c:forEach>

MapActionjava
Java代码
package comzxdemoaction;

import javautilArrayList;
import javautilHashMap;
import javautilList;
import javautilMap;

import comopensymphonyxwork2ActionSupport;
import comzxdemomodelProduct;
import comzxdemomodelStudent;

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>();
mapput("1", "one");
mapput("2", "two");

studentMap=new HashMap<String,Student>();
studentMapput("student1",new Student(new Long(1),"20034140201","张三1","男",25));
studentMapput("student2",new Student(new Long(2),"20034140202","张三2","女",26));
studentMapput("student3",new Student(new Long(3),"20034140202","张三3","男",27));

arrayMap=new HashMap<String,String[]>();
arrayMapput("arr1", new String[]{"1","2003401","leejie","male","20"});
arrayMapput("arr2", new String[]{"2","2003402","huanglie","male","25"});
arrayMapput("arr3", new String[]{"3","2003403","lixiaoning","male","21"});
listMap=new HashMap<String,List<Student>>();

List<Student> list1=new ArrayList<Student>();
list1add(new Student(new Long(1),"20034140201","张三1","男",25));
list1add(new Student(new Long(2),"20034140202","张三2","男",25));
list1add(new Student(new Long(3),"20034140203","张三3","男",25));
listMapput("class1", list1);

List<Student> list2=new ArrayList<Student>();
list2add(new Student(new Long(1),"20034140301","李四1","男",20));
list2add(new Student(new Long(2),"20034140302","李四2","男",21));
list2add(new Student(new Long(3),"20034140303","李四3","男",22));
list2add(new Student(new Long(4),"20034140304","李四4","男",23));
listMapput("class2", list2);
return SUCCESS;

}
public Map<String, String> getMap() {
return map;
}

public void setMap(Map<String, String> map) {
thismap = map;
}

public Map<String, Student> getStudentMap() {
return studentMap;
}

public void setStudentMap(Map<String, Student> studentMap) {
thisstudentMap = studentMap;
}
public Map<String, String[]> getArrayMap() {
return arrayMap;
}
public void setArrayMap(Map<String, String[]> arrayMap) {
thisarrayMap = arrayMap;
}
public Map<String, List<Student>> getListMap() {
return listMap;
}
public void setListMap(Map<String, List<Student>> listMap) {
thislistMap = listMap;
}
}
2testMapjsp
Java代码
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>struts2中的map遍历总结</title>
</head>
<body>
<b>1map中的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>2map中的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="valueid"/></td>
<td><s:property value="valuenum"/></td>
<td><s:property value="valuename"/></td>
<td><s:property value="valuesex"/></td>
<td><s:property value="valueage"/></td>
</tr>
</s:iterator>
</table>
<p>
<b>3map中的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>4map中的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>

<s:iterator value="listMap" id="column">
<s:set name="total" value="#columnvaluesize"/>
<s:iterator value="#columnvalue" status="s">
<tr>
<s:if test="#sfirst"><td rowspan="${total}"><s:property value="#columnkey"/></td></s:if>
<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>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13384855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-26
下一篇 2023-07-26

发表评论

登录后才能评论

评论列表(0条)

保存