JSP/Servlet – JSTL <c:forEach> 커스텀 액션 사용하기
<c:forEach> 커스텀 액션은 자바의 for문에 해당하는 기능을 제공하는 커스텀 액션입니다.
<c:forEach begin="시작값" end="끝값">
실행할 구문
</c:forEach>
아니면 카운터 변수를 사용하고 싶다면?
<c:forEach var"카운터변수" begin="시작값" end="끝값">
실행할 구문
</c:forEach>
증가치를 사용하고 싶다면
<c:forEach var"카운터변수" begin="시작값" end="끝값" step="증가치">
실행할 구문
</c:forEach>
forEach를 사용하는 간단 예제를 만들어 봅시다.
forEach.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:forEach var="cnt" begin="1" end="5">
<FONT size=${cnt } > 가나다라 </FONT>
</c:forEach>
</body>
</html>
실행 화면
가나다라가 점점 커지는 것을 볼 수 있습니다.
<c:forEach>를 사용해서 배열의 값을 출력하고 싶다면 다음과 같이 하면됩니다.
<c:forEach var="저장할 변수" items="${배열의이름}">
${저장할 변수}
</c:forEach>
배열을 출력하는 예제를 위해 Count.jsp와 CountView.jsp를 만들어야 합니다.
Count.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String arr[] = {"1", "2", "3"};
request.setAttribute("CNT", arr);
%>
<jsp:forward page="CountView.jsp"/>
CountView.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:forEach var="cnt" items="${CNT }">
<LI> ${cnt} </LI>
</c:forEach>
</body>
</html>
실행 화면
배열의 값이 제대로 출력됨을 알 수 있습니다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet – JSTL <c:url> 커스텀 액션 사용 하기 (1) | 2016.01.30 |
---|---|
JSTL/Servlet – JSTL <c:redirect> 커스텀 액션 사용하기 (0) | 2016.01.30 |
JSTL/Servlet – JSTL <c:catch> 커스텀 액션 사용 하기 (0) | 2016.01.30 |
JSP/Servlet – JSTL <c:forTokens> 커스텀 액션 사용하기 (0) | 2016.01.30 |
JSP/Servlet – JSTL <c:choose> 커스텀 액션 사용하기 (0) | 2016.01.25 |
JSP/Servlet - JSTL <c:if> 커스텀 액션 사용하기 (0) | 2016.01.24 |
JSP/Servlet – JSTL <c:set> 커스텀 액션 사용하기 (0) | 2016.01.24 |
JSP/Servlet – 스크립팅 요소를 대신하는 표준액션들 (0) | 2016.01.19 |