jsp页面如何对map集合遍历
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp页面如何对map集合遍历相关的知识,希望对你有一定的参考价值。
Map<Class,List<Student>> mapSet= new HashMap<Class,List<Student>>();
map集合中,key的值是班级class, value是每个班级对应的学生集合,现在想在页面显示不知道如何遍历map
页面效果应如下:(和贴吧差不多)
班级一:
学生A
学生B
学生C
班级二:
学生A1
学生B1
学生C1
先在我这样写,但是不对,items="$item.key"中key取不到值
<c:forEach items="$mapSet" var="item">
<c:forEach items="$item.key" var="var">
$var.name
</c:forEach>
<c:forEach items="$item.value" var="s">
$s.name
</c:forEach>
</c:forEach>
请大神给修改一下jsp页面写法
Java代码
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代码
<%@ 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" 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" 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" 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="#column.value.size"/>
<s:iterator value="#column.value" status="s">
<tr>
<s:if test="#s.first"><td rowspan="$total"><s:property value="#column.key"/></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> 参考技术A $item.key中的key的值是你定义的一个班级Class对象,它不是可遍历的对象。
应该把
<c:forEach items="$item.key" var="var">
$var.name
</c:forEach>
改为
$item.key.name追问
问题已经解决,你说的也对
本回答被提问者和网友采纳jsp页面如何遍历数据库的表
本人为初学者,以下是本菜鸟在实际运用时的记录,大神可以跳过,当然也欢迎指点一二
- 从JSP页面开始讲起,在此用的是
<s:iterator var="word" value="#wordList>//其中wordList是与Action中context.put("wordList", wordList);里的list集合相对应的,list装的是数据库东西,word只是个变量名 <s:property value="#word.details"/>//用<s:property取出wordList元素中的details属性, 即数据库中的details字段 </s:iterator>
- 现在来到action
private String time;
private String details;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String showWordList()throws Exception{
ActionContext context=ActionContext.getContext();
List<Word> wordList=WordDao.getWordList();//List集合接收的是从Dao层传来的数据库内容
context.put("wordList", wordList);
return "word";
}
- 然后就是Dao层
public static List<Word> getWordList() { Session session=HibernateSessionFactory.getSession(); try { Criteria criteria=session.createCriteria(Word.class); List<Word> word=criteria.list();//获取数据库里的表装到List集合中 session.close(); return word;//返回list集合 } catch (Exception e) { e.printStackTrace(); } return null; }
- 接着就是model层
public class Word implements java.io.Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String time; private String details; public Word() { } //重载构造方法 public Word(String time, String details) { this.time = time; this.details = details; } ...下面省略各个成员的set,get方法 }
现在是hbm.xml,与hibernate.cfg.xml相关文件的配置
//以下是hbm.xml <hibernate-mapping> <!-- 映射数据库的word表 --> <class name="com.model.Word" table="word" catalog="se"> <!-- 映射id字段 --> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="identity" /> </id> <!-- 映射name字段 --> <property name="time" type="java.lang.String"> <column name="time" length="30" not-null="true" /> </property> <!-- 映射pwd字段 --> <property name="details" type="java.lang.String"> <column name="details" length="2000" not-null="true" /> </property> </class> </hibernate-mapping> //以下是cfg.xml <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 链接地址 --> <property name="connection.url"> jdbc:mysql://localhost:3306/se?useUnicode=true&characterEncoding=UTF-8 </property> <!-- 数据库user --> <property name="connection.username">root</property> <!-- 数据库user密码 --> <property name="connection.password">root</property> <!-- 连接driver --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 映射文件 --> <mapping resource="com/model/Word.hbm.xml" /> </session-factory> </hibernate-configuration>
- 接着是struts.xml的配置
<action name="wordpage" class="com.action.WordAction">
<result name="word">/user/word.jsp</result>
</action>