随笔-37  评论-58  文章-4  trackbacks-0

以前都用forEach标签迭代List,Set对象,今天需要用它来迭代Map对象,就研究了一下。

以下是试验代码:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%!
 public static class TTT{
  private String name;
  public TTT(String name){
   this.name = name;
  }
  public String getName(){
   return name;
  }
 }
%>
<%
 Map map = new LinkedHashMap();
 map.put(new TTT("111"),"aaaaaa");
 map.put(new TTT("222"),"bbbbbb");
 request.setAttribute("map",map);
%>
<body>
<c:forEach items="${map}" var="item">
${item.key.name}-${item.value}<br/>
</c:forEach>
</body>
</html>

item内保存的是java.util.Map.Entry对象这个对象有getKey,setKey,getValue,setValue方法,这样就可以在forEach内部使用map的key和value了。

posted on 2008-09-25 09:28 枫中玎玲 阅读(524) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: