c_foreach

<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}&nbsp;${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}&nbsp;
	</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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值