Java/JSTL/Loop
Содержание
- 1 Count to 10 Example: tracking even and odd
- 2 Count to 10 Example using JSTL
- 3 JSTL: another for each and status
- 4 JSTL: Conditional Support -- Simple Conditional Execution Example
- 5 JSTL For Each
- 6 JSTL: for each and scoped variable
- 7 The forEach tag exposes a scoped variable called "count", which is the position of the current iteration of the collection.
- 8 The forEach tag exposes a scoped variable called "count", which is the position of the current iteration of the collection.
- 9 List of people
Count to 10 Example: tracking even and odd
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %> <html>
<head> <title>Count to 10 Example(tracking even and odd)</title> </head> <body>
From 1 to 10<c:forEach var="i" begin="1" end="10"> <c:out value="${i}" /> |
From 10 to 1<c:forEach var="i" begin="1" end="10"> <c:out value="${11-i}" /> |
By Twos<c:forEach var="i" begin="2" end="10" step="2"> <c:out value="${i}" /> |
</body>
</html>
</source>
JSTL: another for each and status
<source lang="java">
<%@ taglib prefix="c" uri="http://java.sun.ru/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.
<c:forEach var="currentName" items="${pageScope.names}" varStatus="status"> Family member #<c:out value="${status.count}" /> is <c:out value="${currentName}" />
</c:forEach> </body>
</html>
</source>
JSTL: Conditional Support -- Simple Conditional Execution Example
<source lang="java">
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <html> <head>
<title>JSTL: Conditional Support -- Simple Conditional Execution Example</title>
</head> <body bgcolor="#FFFFFF">
Loop in JSTL
<c:forEach var="i" begin="1" end="10" step="1"> <c:out value="${i}" />
</c:forEach>
</body> </html>
</source>
JSTL For Each
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>Reading RSS</title> </head> <body> <c:import var="news" url="http://www.wired.ru/news_drop/netcenter/netcenter.rdf" /> <x:parse var="doc" xml="${news}" />
<x:out select="$doc/rss/channel/title" /> |
|||||||||||||||||
<x:out select="$doc/rss/channel/description" /> |
|||||||||||||||||
JSTL: for each and scoped variable<source lang="java"> <%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Joe, Zhou" scope="page" /> <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 iteration of the collection.(Note, it is not the position of the element in the underlying collection)<c:forEach items="${pageScope.names}" var="currentName" varStatus="status" > Family member #<c:out value="${status.count}" /> is <c:out value="${currentName}" /> </html> </source>
JSTL: for each and status<source lang="java"> <%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Joe, Rob, Rosy, Sissi" scope="page" /> <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 iteration of the collection.(Note, it is not the position of the element in the underlying collection)<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}" /> </html>
</source>
JSTL: for each loop<source lang="java"> <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/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> List of people
</BODY> </HTML>
</source>
JSTL Form Value and ForEach Loop<source lang="java"> <html> <head> <title>Page Data Example</title> </head> <body>
</body> </html> ///////////////////////////////////////////////////////////////////////////////////// //File: params2.zip </source>
JSTL: fortokens<source lang="java"> <%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Joe:Petter;Ryan|John" scope="page" /> <html> <head> <title>forTokens action</title> </head> <body> <c:forTokens items="${pageScope.names}" delims=":;|" var="currentName" varStatus="status" > Family member #<c:out value="${status.count}" /> is <c:out value="${currentName}" /> </html>
</source>
JSTL Tag collaboration with a fixed loop<source lang="java"> <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %> <html> <head> <title>Count to 10 Example(tracking even and odd)</title> </head> <body> |