JSP JSTL <c:forEach>태그1

MINJU KIM·2023년 12월 11일

JSP

목록 보기
26/30

<c:forEach>

begin="시작값" end="마지막값" step="증가값" varStatus="반복상태 변수명">
...
</c:forEach>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%--추가해준다--%>
<html>
<head>
    <title>JSTL- forEach 1 일반 for문 형태의 forEach 태그</title>
</head>
<body>
<h4>일반 for문 형태</h4>
<c:forEach begin="1" end="3" step="1" var="i">
    <p>반복 ${ i }입니다.</p>
</c:forEach>

<h4>varStatus 속성 살펴보기</h4>
<table border="1">
    <c:forEach begin="3" end="5" var="i" varStatus="loop">

        <tr>
            <td>count : ${ loop.count }</td>
            <td>index : ${ loop.index }</td>
            <td>current : ${ loop.current }</td>
            <td>first : ${ loop.first }</td>
            <td>last : ${ loop.last }</td>
        </tr>
    </c:forEach>
</table>

<h4>1에서 100까지 정수 중 홀수의 합</h4>
<c:forEach begin="1" end="100" var="j">
    <c:if test="${ j mod 2 ne 0}">
        <c:set var="sum" value="${ sum + j }"></c:set>
    </c:if>

</c:forEach>
    1~100사이의 정수 중 홀수의 합은? ${ sum }



</body>
</html>

결과

https://lifejusik1004.tistory.com/entry/JSP-JSTL-cforEach-%ED%83%9C%EA%B7%B8-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0
https://web-inf.tistory.com/14

0개의 댓글