81,122
社区成员




<%@page contentType="text/html; charset=GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.sun.tools.jdi.LinkedHashMap"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Map m = new LinkedHashMap();
m.put("1","a");
m.put("2","b");
m.put("3","c");
m.put("4","d");
request.setAttribute("map",m);
%>
<c:forEach items="${map}" var="item" varStatus="s">
${item.key }:${item.value }<br/>
</c:forEach>
</body>
</html>
<c:forEach items="${map}" var="item" varStatus="s">
${item[s.index]}
</c:forEach>
[code]
首先,map是不可能像你这样循环的,应该用${item.key}和${item.value}来取值。
第二,即使是可以循环也应该写成${map[s.index]},而不是${item[s.index]}
第三,s.index可以直接作为整数使用。
请看下面的例子:
[code=HTML]
<%@page contentType="text/html; charset=GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Map m = new HashMap();
m.put("1","a");
m.put("2","b");
m.put("3","c");
m.put("4","d");
request.setAttribute("map",m);
List l = new ArrayList();
l.add("1");
l.add("2");
l.add("3");
request.setAttribute("l",l);
%>
<c:forEach items="${l}" var="item" varStatus="s">
${l[s.index]}
</c:forEach>
</body>
</html>