[JSTL이란]

JSTL 은 JSP(Java Server Pages) Standard Tag Library의 약자로써, jsp 어플리케이션의 핵심기능을 캡슐화한 유용한 태그 모음.

 

tutorial 사이트 :

https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

 

<JSTL 사용을 위한 taglib 선언>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

사용하고자 하는 태그라이브러리(taglib)의 prefix 를 각각 선언

 

 

<JSTL tag 별 용도 및 설명>

1.c tag : 코어

c:if : 조건문 

c:choose c:when c:otherwise : switch case default 조건문

c:forEach : 반복문

c:set : 변수 할당

c:out : 출력

2.fn tag : 길이반환 등의 함수

fn:length : 길이 반환

fn:substring : 문자열 자르기

3.fmt tag : format 변환용으로 사용

아래 url 참고

https://www.javatpoint.com/jstl-formatting-tags

 

 

<사용 예>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<c:if test="${ not empty dataList}">
     <c:forEach var="list" items="${dataList}" varStatus="status">
        <tr class="dataTr tmpTr" style='cursor:pointer;' onclick="goInfo('${list.seq}')">
            <td>${list.start_page_limit + status.count}</td>
            <td>${list.seq}</td>
            <td>
            <c:choose>
               <c:when test="${fn:length(list.memo)>20}">
                  <c:out value="${fn:substring(list.memo, 0, 19)}"/>...
               </c:when>
               <c:otherwise>
                  <c:out value="${list.memo}"/>
               </c:otherwise>
            </c:choose>
            </td>
            <td>
            <c:set var="nm" value="${fn:split(list.user_name, '^')}" />
            <c:forEach var="obj" items="${nm}" varStatus="idx">
              ${obj}<c:if test="${!idx.last}">,<br></c:if>
            </c:forEach> 
            </td>
        </tr>
    </forEach>
</c:if>
cs

1 line : 서버로부터 받아온 dataList 가 비어있지 않다면 2~23 라인 실행

2 line : 반복문

3 line : 서버에서 받아온 dataList 를 list 변수에 담아 for 문 내에서 사용. varStatus 는 index 와 관련되어 사용이 가능한 속성

4 line : varStatus.count로 순번 출력(1부터 출력) (참고: varStatus.index는 0부터 출력)

7 ~ 14 line : if else 구문

8 line : list에서 가져온 memo 의 길이가 20 보다 큰 경우

9 line : 0~19번째 문자까지 출력 후 ... 붙이기 (말줄임)

17 line : nm 변수에 list 에서 가져온 user_name 을 ^ 문자 기준으로 split 하여 저장

18 line : nm 변수를 obj 변수에 담아 for 문 내에서 사용

19 line : 마지막이 아닌 경우 (!varStatus.last) ,<br> 을 붙임 (반대로 다시말해 마지막인 경우 ,<br> 을 붙이지 않음) 

 

 

< foreach 의 varStatus >

<c:forEach var='list' items='${dataList}' varStatus='status'>

   ${status.current} // 현재아이템

   ${status.index}   // 0부터 시작하는 인덱스

   ${status.count}   // 1부터 시작하는 카운트

   ${status.first}   // 현재 인덱스가 첫번째 인덱스인지 판별

   ${status.last}    // 현재 인덱스가 마지막 인덱스인지 판별

   ${status.begin}   // 반복문 시작 값

   ${status.end}     // 반복문 끝 값

   ${status.step}    // 반복문 증가 값

</c:forEach>

 

참고 : https://www.javatips.net/blog/jstl-last-using-varstatus

 

 

< 주의사항 > 

페이지 로드 순서(웹언어들의 동작 및 실행 순서(Load Sequence On Web))에 주의하자..

JAVA > JSTL > HTML > JavaScript(jQuery)

 

 

해당 부분은 따로 정리해서 포스팅하겠다.

 

반응형

+ Recent posts