1. foreach loop 기본
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Count to 10 Example(using JSTL)</title>
</head>
<body>
<c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${i}" />
<br />
</c:forEach>
</body>
</html>
2. Begin, End 스텝을 이용한 foreach
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:set var="names" value="Peter, Pat, Mark, Tracy" 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>
3. Integer 를 이용하기 위한 foreach loop
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Count to 10 Example(using JSTL)</title>
</head>
<body>
<table border="1">
<tr>
<td valign="top">
<h3>From 1 to 10</h3>
<c:forEach var="i" begin="1" end="10">
<c:out value="${i}" />
<br />
</c:forEach>
</td>
<td valign="top">
<h3>From 10 to 1</h3>
<c:forEach var="i" begin="1" end="10">
<c:out value="${11-i}" />
<br />
</c:forEach>
</td>
</tr>
</table>
</body>
</html>
4. 스트링 처리를 위한 forEach 태그
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>
<%
String[] bikes = { "A", "B", "C" };
%>
<!-- application.setAttribute("bikeList", bikes); -->
<HTML>
<HEAD><TITLE>JSTL 'forEach' tag</TITLE></HEAD>
<BODY>
Here are my bikes:
<UL>
<c-rt:forEach var="bike" items="<%= bikes %>">
<LI><c:out value="${bike}" />
</c-rt:forEach>
</UL>
</BODY>
</HTML>
5. forEach에서 status Count 처리
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:set var="names" value="A B C, D" scope="page" />
<html>
<head>
<title>forEach and status</title>
</head>
<body>
<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>
6. Step 를 이용한 forEach 문
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Count to 10 Example(using JSTL)</title>
</head>
<body>
<table border="1">
<tr>
<td>
<h3>By Twos</h3>
<c:forEach var="i" begin="2" end="10" step="2">
<c:out value="${i}" />
<br />
</c:forEach>
</td>
<td valign="top">
</td>
</tr>
</table>
</body>
</html>
7. 인덱스에 의한 배열 참조
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>
<%!
String[] names = { "A","B", "C", "D" };
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>
8. 콤마 딜리미터를 처리하기 위한 forEach
<%@ 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>
<c:set var="days" value="A,B,C,D,E,F,G" />
<table border="0">
<c:forEach var="i" items="${days}" 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="#cccccc" />
</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>
9. ArrayList를 이용한 forEach
<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%
// Create an ArrayList with test data
ArrayList list = new ArrayList();
Map author1 = new HashMap();
author1.put("name", "A");
author1.put("id", new Integer(1));
list.add(author1);
Map author2 = new HashMap();
author2.put("name", "B");
author2.put("id", new Integer(2));
list.add(author2);
Map author3 = new HashMap();
author3.put("name", "C");
author3.put("id", new Integer(3));
list.add(author3);
pageContext.setAttribute("authors", list);
%>
<html>
<head>
<title>Search result: Authors</title>
</head>
<body bgcolor="white">
Here are all authors matching your search critera:
<table>
<TH>Name</th>
<TH>Id</th>
<c:forEach items="${authors}" var="current">
<tr>
<td><c:out value="${current.name}" /><td>
<td><c:out value="${current.id}" /><td>
</tr>
</c:forEach>
</table>
</body>
</html>
10. 벡터를 통한 forEach 문
<html>
<head>
<title>Tag Plugin Examples: forEach</title>
</head>
<body>
<h1>Tag Plugin Examples - <c:forEach></h1>
<hr>
<font color="#000000"/>
</br>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="java.util.Vector" %>
<% Vector v = new Vector();
v.add("One"); v.add("Two"); v.add("Three"); v.add("Four");
pageContext.setAttribute("vector", v);
%>
<h3>Iterating over a Vector</h3>
<c:forEach items="${vector}" var="item" >
<c:out value="${item}"/>
</c:forEach>
</body>
</html>
11. forEach에서 홀수 처리하기
<%@ 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>
'WEB_Programming > JSTL' 카테고리의 다른 글
8. JSTL Set (0) | 2008.06.20 |
---|---|
7. JSTL Collection 예제 (0) | 2008.06.20 |
5. JSTL ForTokens 예제 (0) | 2008.06.20 |
4. JSTL Choose 예제 (8) | 2008.06.20 |
3. JSTL IF 조건문 (0) | 2008.06.20 |