JSP~~JSTL~~Core Label Library~~Use of Circular Label forEach

forEach iteration tag: This tag is used to iterate over a series of objects in a Collection set and can specify the number of iterations.

The general format used is as follows:

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

The attributes used in this tag are described below:

var: The name of the object stored in the Collection collection class.

items: The name of the collection class to iterate over.

varStatus: Stores the state information of an iteration and has access to the information of the iteration itself.

Begin: If a begin value is specified, the iteration starts from items[begin], and if no begin value is specified, the iteration starts from the first value of the set.

End: Indicates the end of the iteration to the end bit of the set, or the last bit of the set in the iteration until the end value is specified.

Step: Specifies the step of the iteration.

Here are two examples of using forEach

<%@ page contentType="text/html;charset=GB2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
	<title>c:forEach Use examples</title>
</head>
<body>
<%
//------------------ Sets the initial value of an array-------------
String name[] = new String[4];
name[0] = "Guo Jingjing";
name[1] = "Zhang Yining";
name[2] = "He Wenna";
name[3] = "Marine";
request.setAttribute("name", name);
%>
<!---------use forEach Output Array Element Value and Related Attributes in Iteration-->
<c:forEach items="${name}" var="currentName"
	varStatus="currentVarStaus">
	The name of the current champion:<c:out value="${currentName}" />;
	The current champion index number is:<c:out value="${currentVarStaus.index}" />;<br>
	Current Total Iteration<c:out value="${currentVarStaus.count}" />second;
	<c:if test="${currentVarStaus.first}">Currently is the first iteration operation</c:if>
	<c:if test="${currentVarStaus.last}">Currently the last iteration operation</c:if>
	<hr>
</c:forEach>
</body>
</html>

The result of running the display is:

The name of the current champion is: Guo Jingjing; the index number of the current champion is: 0;
Currently iterates once in total; currently is the first iteration operation 
--------------------------------------------------------------------------------
The name of the current champion is Zhang Yining; the index number of the current champion is: 1;
The current total number of iterations is 2; 
--------------------------------------------------------------------------------
The name of the current champion is: He Wenna; the index number of the current champion is: 2;
The current total number of iterations is 3; 
--------------------------------------------------------------------------------
The name of the current champion is Marine; the current champion index number is 3;
Currently a total of 4 iterations; currently the last iteration operation 
--------------------------------------------------------------------------------

A second example will provide an in-depth introduction to the use of bengin and end

<%@ 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 Label Instance</title>
</head>
<body>
<h4><c:out value="forEach Example"/></h4>
<hr>
<%
String items[] = new String[5];
items[0] = "Core Label Library";
items[1] = "International Label Library";
items[2] = "SQL Label Library";
items[3] = "XML Label Library";
items[4] = "Function Label Library";
request.setAttribute("items",items);
%>
<B><c:out value="Not specified begin and end Iteration of:" /></B><br>
<c:forEach var="item" items="${items}">
  <c:out value="${item}"/><br>
</c:forEach>
<B><c:out value="Appoint begin and end Iteration of:" /></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="Output information for the entire iteration:" /></B><br>
<c:forEach var="item" items="${items}" begin="3" end="4" step="1" varStatus="s">
<c:out value="${item}" />Four attributes:<br>
Location, i.e. index:<c:out value="${s.index}" /><br>
Total number of iterations:<c:out value="${s.count}" /><br>
Is it the first position:<c:out value="${s.first}" /><br>
Is it the last position:<c:out value="${s.last}" /><br>
</c:forEach>
</body>
</html>

The result of running the test is:

forEach instance

--------------------------------------------------------------------------------
Iterations for begin and end are not specified:
 Core Label Library
 International Label Library
 SQL Label Library
 XML Label Library
 Function Label Library
 Specify iterations for begin and end:
 International Label Library
 SQL Label Library
 XML Label Library
 Output information for the entire iteration:
 Four properties of the XML tag library:
  Location, i.e. index: 3
  Total number of iterations: 1
  Is this the first location:true
  Is it the last location: false
 Four properties of the function label library:
  Location, i.e. index:4
  Total number of iterations: 2
  Is it the first position: false
  Is it the last location:true

 

Published 55 original articles. Praise 27. Visits 2008
Private letter follow

Keywords: xml SQL Java JSP

Added by evan12 on Tue, 28 Jan 2020 05:28:02 +0200