当先锋百科网

首页 1 2 3 4 5 6 7

------------------------------------- JSTL标签forEach----------------------------------

forEach(重点)

 

  forEach当前就是循环标签了,下面例子中设置域变量i,初始值为1,结束值为100。即i的值从1遍历到100,包含100。

<c:set var="sum" value="0" />

<c:forEach var="i" begin="1" end="10">

    <c:set var="sum" value="${sum + i}" />

</c:forEach>

<c:out value="sum = ${sum }"/>

<c:set var="sum" value="0" />

<c:forEach var="i" begin="1" end="10" step="2">

    <c:set var="sum" value="${sum + i}" />

</c:forEach>

<c:out value="sum = ${sum }"/>

 

  还可以使用forEach来遍历数据或集合,遍历数组:

<%

String[] names = {"zhangSan", "liSi", "wangWu", "zhaoLiu"};

pageContext.setAttribute("ns", names);

%>

<c:forEach var="item" items="${ns }">

    <c:out value="name: ${item }"/><br/>

</c:forEach>

 

遍历List

<%

    List<String> names = new ArrayList<String>();

    names.add("zhangSan");

    names.add("liSi");

    names.add("wangWu");

    names.add("zhaoLiu");

    pageContext.setAttribute("ns", names);

%>

<c:forEach var="item" items="${ns }">

    <c:out value="name: ${item }"/><br/>

</c:forEach>

 

遍历Map

<%

    Map<String,String> stu = new LinkedHashMap<String,String>();

    stu.put("number", "N_1001");

    stu.put("name", "zhangSan");

    stu.put("age", "23");

    stu.put("sex", "male");

    pageContext.setAttribute("stu", stu);

%>

<c:forEach var="item" items="${stu }">

    <c:out value="${item.key }: ${item.value }"/><br/>

</c:forEach>

 

forEach标签还有一个属性:varStatus,这个属性用来指定接收“循环状态”的变量名,例如:<forEach varStatus=”vs” …/>,这时就可以使用vs这个变量来获取循环的状态了。

current当前这次迭代的(集合中的)项
index当前这次迭代从 0 开始的迭代索引
count当前这次迭代从 1 开始的迭代计数
first用来表明当前这轮迭代是否为第一次迭代的标志
last用来表明当前这轮迭代是否为最后一次迭代的标志

<c:forEach var="item" items="${ns }" varStatus="vs">

    <c:if test="${vs.first }">第一行:</c:if>

    <c:if test="${vs.last }">最后一行:</c:if>

    <c:out value="${vs.count }: "/>

    <c:out value="[${vs.index }]: "/>

    <c:out value="name: ${vs.current }"/><br/>

</c:forEach>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@page import="com.rl.model.*" %>

<%@page import="java.util.*" %>

<!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>

       <%

              String[] strArr = {"likunpeng","zhangsan","lisi"};

              pageContext.setAttribute("strArr", strArr);

              Person p = new Person();

              p.setAge(20);

              p.setName("likunpeng");

              Person p1 = new Person();

              p1.setAge(21);

              p1.setName("zhangsan");

              Person p2 = new Person();

              p2.setAge(22);

              p2.setName("lisi");

              List<Person> pList = new ArrayList<Person>();

              pList.add(p);

              pList.add(p1);

              pList.add(p2);

              pageContext.setAttribute("pList",pList);

              Map<String,String> map = new HashMap<String,String>();

              map.put("name","likunpeng");

              map.put("age","23");

              map.put("job","www.likunpeng.com");

              pageContext.setAttribute("map",map);

        %>

       <h1>JSTL标签forEach</h1>

       <c:out value="${'数值循环:' }"></c:out>

       <c:forEach begin="1" end="10" var="i">

              <c:out value="${i }"></c:out>

       </c:forEach>

       <%-- step设置数值的跨度 --%>

       <c:forEach begin="1" end="10" var="i" step="2">

              <c:out value="${i }"></c:out>

       </c:forEach>

       <br>

       <c:out value="${'字符串数组遍历:' }"></c:out>

       <c:forEach items="${strArr }" var="str">

              <c:out value="${str }"></c:out>

       </c:forEach>

       <br>

       <c:out value="${'List集合遍历:' }"></c:out><br>

       <c:forEach items="${pList }" var="person">

              <c:out value="${person }"></c:out><br>

       </c:forEach>

       <c:forEach items="${pList }" var="person">

              <c:out value="姓名:${person.name } 年龄:${person.age }"></c:out><br>

       </c:forEach>

       <br>

       <c:out value="${'Map集合遍历:' }"></c:out><br>

       <c:forEach items="${map }" var="m">

              <c:out value="${m.key }=${m.value }"></c:out><br>

       </c:forEach>

       <br>

       <c:forEach items="${pList }" var="person" varStatus="status">

              <c:out value="${status.current }"></c:out><br>

              <%--index是从0开始,count不同的是从1开始计算 --%>

              <c:out value="${status.index }相当于${status.count }"></c:out>

              <br>

              <c:if test="${status.first }">

                     <c:out value="是第一行"></c:out><br>

              </c:if>

              <c:if test="${status.last }">

                     <c:out value="是最后一行"></c:out><br>

              </c:if>

       </c:forEach>

</body>

</html>