JSP 를 사용할때

HTML 내에서 for 구문을 사용하고 싶다면 JSTL 의 c:forEach 를 사용하면 된다. 

 

크게 두가지로 많이 사용할 것 같다.

첫번 째, 화면내에서 반복되는 객체(테이블, 버튼 등)를 보여줄때 

두번 쨰, 서버에서 가져온 데이터를 보여줄때(게시판 등)

 

여기서는 첫번째 예를 들어보자.

두번째는 다음에...

 

 

사용법>

<!-- 임의의 시작값, 마지막 값 -->

<c:forEach var="변수" begin="시작값" end="마지막값">

    내용 구현

</c:forEach>

 

<!-- 임의의 시작 값, 마지막 값과 증가값(Option)을 부여할때 -->

<c:forEach var="변수" begin="시작값" end="마지막값" step="증가값">

    내용 구현

</c:forEach>

 

 

사용예 1>

<!-- 변수가 i, 1 부터 10까지 출력하기 -->

<c:forEach var="i" begin="1" end="10">

    i=${i}<br>

</c:forEach>

 

 

출력결과>

i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10

 

 

사용예 2>

<!-- 변수가 i, 1 부터 10까지 2씩 증가 출력하기 -->

<c:forEach var="i" begin="1" end="10" step="2">

    i=${i}<br>

</c:forEach>

 

 

출력결과>

i=1
i=3
i=5
i=7
i=9

 

사용예 3>

테이블 10개 만들기

<c:forEach var="i" begin="1" end="10">

    테이블 <c:out value="${i}"></c:out><br>

    <table id="tbl_{i}" border="1" style="width: 30%">

    <tr>

    <th>가</th>

    <th>나</th>

    <th>다</th>

    </tr>

    <tr>

    <td>a</td>

    <td>b</td>

    <td>c</td>

    </tr>

    </table>

    <br>

</c:forEach>

 

 

출력결과>

생략, 위의 소스 그대로 복사하면 됨

 

 

옵션 설명>

var

Name of the exported scoped variable for the current item of 

 the iteration. This scoped variable has nested visibility. Its 

 type depends on the object of the underlying collection.

 

begin

If items specified: Iteration begins at the item located at the 

 specified index. First item of the collection has index 0. If 

 items not specified: Iteration begins with index set at the 

 value specified.

 

end

If items specified: Iteration ends at the item located at the 

 specified index (inclusive). If items not specified: Iteration 

 ends when index reaches the value specified.

 

step

Iteration will only process every step items of the collection, 

 starting with the first one.

 

items

Collection of items to iterate over.

 

varStatus

Name of the exported scoped variable for the status of the 

iteration. Object exported is of type 

javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable 

has nested visibility.

 
index, count 사용가능 함으로 편리 할것 같다.

 

출처>

이클립스 JSTL 라이브러리

 

참고>
https://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/forEach.html

 

 

광고>

+ Recent posts