<c:forEach/>标签
增强的for循环,
格式:
<c:forEach items="" var="" />
items:需要遍历的对象
var:用于存放遍历的每一项内容,
存放在page作用域,只能在循环体中使用
1、items为字符串或字符串数组
为字符串,直接输出,为字符串数组,遍历输出2、items为list集合
3、items为map集合
4、普通for循环
begin:从1开始
end:到10结束
step:每次加2
var:循环的变量,
<!-- 循环输出所有商品 -->
<c:forEach items="${ps}" var="p">
<tr onmouseover="this.style.backgroundColor = 'white'"
onmouseout="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="200">${p.id }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="18%">${p.name }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="8%">${p.price }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="8%">${p.pnum }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">${p.category}</td>
<td align="center" style="HEIGHT: 22px" width="7%">
<a href="${pageContext.request.contextPath}/findProductById?id=${p.id}&type=admin">
<img src="${pageContext.request.contextPath}/admin/images/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px" width="7%">
<a href="${pageContext.request.contextPath}/deleteProduct?id=${p.id}" onclick="javascript:return p_del()">
<img src="${pageContext.request.contextPath}/admin/images/i_del.gif"
width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td>
</tr>
</c:forEach>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head></head>
<body>
<%
String [] fruits={"apple","orange","grape","banana"};
%>
String数组中的元素:<br>
<c:forEach var="name" items="<%=fruits%>">
${name}<br>
</c:forEach>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head></head>
<body>
<%
Map userMap = new HashMap();
userMap.put("Tom", "123");
userMap.put("Make","123");
userMap.put("Lina","123");
%>
HashMap集合中的元素:<br>
<c:forEach var="entry" items="<%=userMap%>">
${entry.key} ${entry.value}<br>
</c:forEach>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head></head>
<body>
colorsList集合(指定迭代范围和步长)<br>
<%
List colorsList=new ArrayList();
colorsList.add("red");
colorsList.add("yellow");
colorsList.add("blue");
colorsList.add("green");
colorsList.add("black");
%>
<c:forEach var="color" items="<%=colorsList%>" begin="1"
end="3" step="2">
${color}
</c:forEach>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head></head>
<body style="text-align: center;">
<%
List userList = new ArrayList();
userList.add("Tom");
userList.add("Make");
userList.add("Lina");
%>
<table border="1">
<tr>
<td>序号</td>
<td>索引</td>
<td>是否为第一个元素</td>
<td>是否为最后一个元素</td>
<td>元素的值</td>
</tr>
<c:forEach var="name" items="<%=userList%>" varStatus="status">
<tr>
<td>${status.count}</td>
<td>${status.index}</td>
<td>${status.first}</td>
<td>${status.last}</td>
<td>${name}</td>
</tr>
</c:forEach>
</table>
</body>
</html>