现在的位置: 首页 > 综合 > 正文

jsp迭代list和map集合

2017年11月09日 ⁄ 综合 ⁄ 共 945字 ⁄ 字号 评论关闭

其中要使用jstl标签库,需要导入jstl必要的jar包

<%@page import="cn.itcast.domain.Person"%>
<%@page import="java.util.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	List list = new ArrayList();
	list.add(new Person("aaa")); 
	list.add(new Person("bbb")); 
	list.add(new Person("ccc")); 
	
	request.setAttribute("list", list);
%>

<c:forEach var="person" items="${list }">
	${person.name } <br>
</c:forEach>
<br>
<%
	Map map = new HashMap();
	map.put("aa",new Person("aa"));
	map.put("bb",new Person("bb"));
	map.put("cc",new Person("cc"));
	map.put("dd",new Person("dd"));
	
	request.setAttribute("map", map);
%>

<c:forEach var="entry" items="${map }">
	${ entry.key}  :${entry.value.name}<br>
</c:forEach>

</body>
</html>

结果:

抱歉!评论已关闭.