Professional Documents
Culture Documents
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:set var="names" value="Joe, Rob, Rosy, Sissi" scope="page" />
<html>
<head>
<title>forEach and status</title>
</head>
<body>
<h1>The forEach tag exposes a scoped variable called 'count', which
is the position of the current iteration of the collection.</h1>
<h2>(Note, it is <i>not</i> the position of the element in the
underlying collection)</h2>
<c:forEach items="${pageScope.names}"
var="currentName"
varStatus="status"
begin="0"
end="3"
step="2"
>
Family member #<c:out value="${status.count}" /> is
<c:out value="${currentName}" /> <br />
</c:forEach>
</body>
</html>
JSTL: another for each and status
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%
synchronized (pageContext) {
String[] names = {"Joe", "Rosy", "Petter", "Rob"};
pageContext.setAttribute("names", names, PageContext.PAGE_SCOPE);
}
%>
<html>
<head>
<title>forEach and status</title>
</head>
<body>
The forEach tag exposes a scoped variable called 'count', which
is the position of the current element within the collection. <br />
<c:forEach var="currentName" items="${pageScope.names}" varStatus="status">
Family member #<c:out value="${status.count}" /> is
<c:out value="${currentName}" /> <br />
</c:forEach>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:set var="names" value="Joe, Zhou" scope="page" />
<html>
<head>
<title>forEach and status</title>
</head>
<body>
<h1>The forEach tag exposes a scoped variable called 'count', which
is the position of the current iteration of the collection.</h1>
<h2>(Note, it is <i>not</i> the position of the element in the
underlying collection)</h2>
<c:forEach items="${pageScope.names}"
var="currentName"
varStatus="status"
>
Family member #<c:out value="${status.count}" /> is
<c:out value="${currentName}" /> <br />
</c:forEach>
</body>
</html>
JSTL: for each loop
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>
<%!
String[] names = { "Joe", "Rosy", "Sissi", "Sun" };
int[] ages = {29, 8, 6, 5};
%>
<HTML>
<HEAD><TITLE>JSTL 'forEach' tag</TITLE></HEAD>
<BODY>
<H1>List of people</H1>
<TABLE BORDER="1">
<TH>Name</TH>
<c-rt:forEach var="person" items="<%= names %>">
<TR>
<TD><c:out value="${person}" /></TD>
<TD><c:out value="${ages[i]}" /></TD>
</TR>
</c-rt:forEach>
</TABLE>
</BODY>
</HTML>
Count to 10 Example: tracking even and odd
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>
<html>
<head>
<title>Count to 10 Example(tracking even and odd)</title>
</head>
<body>
<table border="0">
<c:forEach var="i" begin="1" end="10" varStatus="status">
<jsp:useBean id="status"
type="javax.servlet.jsp.jstl.core.LoopTagStatus" />
<c-rt:choose>
<c-rt:when test="<%=status.getCount()%2==0%>">
<c:set var="color" value="#eeeeee" />
</c-rt:when>
<c-rt:otherwise>
<c:set var="color" value="#dddddd" />
</c-rt:otherwise>
</c-rt:choose>
<tr>
<td width="200" bgcolor="<c:out value="${color}"/>">
<c:out value="${i}" />
</td>
</tr>
</c:forEach>
</table>
</body>
</html>