Use JSP ~~ JSTL ~~ ~~ core tag library circulation of forEach tag

forEach iteration tags: The tags used to iterate outputs a series of objects Collection collection, and can specify the number of iterations.

Using the following general format:

<c:forEach items=”collection” var=”varName” 
     [varstatus=”varStatusName”][begin=”begin”] [end=”end”] [step=”step”]>
     body content
</c:forEach>

This attribute describes tags used are as follows:

var: that is, to save the object name in the Collection collection classes.

items: a set of class names to be iterative.

varStatus: state information is stored iteration, iteration can access to their own information.

begin: If you begin value is specified, it means the beginning of the iteration from items [begin], if you do not specify begin value, a set of values ​​from the first iteration begins.

end: indicates the end of iteration to end the set position, if not specified end value, then the last iteration has been set.

step: Specifies the step size iteration.

Let's look at two cases using forEach

<%@ page contentType="text/html;charset=GB2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
	<title>c:forEach使用示例</title>
</head>
<body>
<%
//----------设置一个数组的初始值----------
String name[] = new String[4];
name[0] = "郭晶晶";
name[1] = "张怡宁";
name[2] = "何雯娜";
name[3] = "马琳";
request.setAttribute("name", name);
%>
<!---------用forEach输出数组元素值及迭代过程中的相关属性-->
<c:forEach items="${name}" var="currentName"
	varStatus="currentVarStaus">
	当前冠军的姓名为:<c:out value="${currentName}" />;
	当前冠军索引号为:<c:out value="${currentVarStaus.index}" />;<br>
	当前共计迭代<c:out value="${currentVarStaus.count}" />次;
	<c:if test="${currentVarStaus.first}">当前是第一次迭代操作</c:if>
	<c:if test="${currentVarStaus.last}">当前是最后一次迭代操作</c:if>
	<hr>
</c:forEach>
</body>
</html>

The results show that the run:

当前冠军的姓名为:郭晶晶; 当前冠军索引号为:0;
当前共计迭代1次; 当前是第一次迭代操作 
--------------------------------------------------------------------------------
当前冠军的姓名为:张怡宁; 当前冠军索引号为:1;
当前共计迭代2次; 
--------------------------------------------------------------------------------
当前冠军的姓名为:何雯娜; 当前冠军索引号为:2;
当前共计迭代3次; 
--------------------------------------------------------------------------------
当前冠军的姓名为:马琳; 当前冠军索引号为:3;
当前共计迭代4次; 当前是最后一次迭代操作 
--------------------------------------------------------------------------------

The second example, the insight into the use and end of bengin

<%@ page contentType="text/html;charset=GBK" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
  <title>JSTL: -- forEach 标签实例</title>
</head>
<body>
<h4><c:out value="forEach 实例"/></h4>
<hr>
<%
String items[] = new String[5];
items[0] = "核心标签库";
items[1] = "国际化标签库";
items[2] = "SQL 标签库";
items[3] = "XML 标签库";
items[4] = "函数标签库";
request.setAttribute("items",items);
%>
<B><c:out value="不指定 begin 和 end 的迭代:" /></B><br>
<c:forEach var="item" items="${items}">
  <c:out value="${item}"/><br>
</c:forEach>
<B><c:out value="指定 begin 和 end 的迭代:" /></B><br>
<c:forEach var="item" items="${items}" begin="1" end="3" step="1">
 <c:out value="${item}" /><br>
</c:forEach>
<B><c:out value="输出整个迭代的信息:" /></B><br>
<c:forEach var="item" items="${items}" begin="3" end="4" step="1" varStatus="s">
<c:out value="${item}" />的四种属性:<br>
所在位置,即索引:<c:out value="${s.index}" /><br>
总共已迭代的次数:<c:out value="${s.count}" /><br>
是否为第一个位置:<c:out value="${s.first}" /><br>
是否为最后一个位置:<c:out value="${s.last}" /><br>
</c:forEach>
</body>
</html>

The results of the test run are:

forEach 实例

--------------------------------------------------------------------------------
不指定 begin 和 end 的迭代:
 核心标签库
 国际化标签库
 SQL 标签库
 XML 标签库
 函数标签库
指定 begin 和 end 的迭代:
 国际化标签库
 SQL 标签库
 XML 标签库
输出整个迭代的信息:
 XML 标签库的四种属性:
  所在位置,即索引:3
  总共已迭代的次数:1
  是否为第一个位置:true
  是否为最后一个位置:false
 函数标签库的四种属性:
  所在位置,即索引:4
  总共已迭代的次数:2
  是否为第一个位置:false
  是否为最后一个位置:true

 

Published 55 original articles · won praise 27 · views 2039

Guess you like

Origin blog.csdn.net/qq_16388171/article/details/104097808