본문 바로가기
카테고리 없음

JSTL(JSP Standard Tag Library)

by epro 2007. 4. 24.

JSTL1.1은 JSP2.0 스펙의 일부가 아니다.
JSTL을 사용하려면, 먼저 웹 애플리케이션의 WEB-INF/lib에 jstl.jar파일을 설치해야 합니다.
설치는 jstl.jar파일을 그냥 WEB-INF/lib에 복사해 두면 됩니다.
(참고로 톰캣5 예제파일에 있는 JSTL의 위치는 webapps/jsp-examples/WEB-INF/lib/jstl.jar 입니다.)


* 자주쓰는 Core 태그 정리 *

1. 조건문

태그명 : <c:if>

<c:if test="${userType eq 'member'}">
  <jsp:include page="inputComments.jsp" />
</c:if>

* userType eq 'member'는 userType == 'member' 혹은 userType == "member"도 가능.
* java와 다른 점은 else가 없다는 것!!!!
  그래서 꽁수로 if문에 else조건을 줘서 if를 여러개 만들어 쓰기도 함.


태그명 :  <c:choose>

<c:choose>
  <c:when test="${userPref=='performance'}">
       //performance에 해당하는 작업 수행
  </c:when>
  <c:when test="${userPref=='safety'}">
       //safety에 해당하는 작업 수행
   </c:when>
  <c:otherwise>
      //위에있는 when에 해당되지 않을 때
  </c:otherwise>
</c:choose>


2. 루핑태그

태그명 : <c:forEach>
기능 : 배열과 컬렉션 데이터를 루프로 돌리는 작업을 간단하게 처리할 수 있습니다.

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

<c:forEach var="movie" items="${movieList}" varStatus="movieLoopCount">
  <tr>
     <td>${movieLoopCount.count}</td>
     <td>${movie}</d>
 </tr>
</c:forEach>

* movie : 컬렉션에 있는 각각 항목을 나타내는 변수, 한번 회전할 때마다 값이 바뀜
* movieList : 루핑을 돌 실제 데이터(배열, 컬렉션, 맵 또는 콤마 분리 문자열)
* movieLoopCount : for문의 i와 같은 것
                            => LoopTagStatus 객체에는  count라는 프로퍼티가 있어
                                지금이 몇번째 회전인지 알 수 있다고 한다.

그 외..
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:formatNumber value='${fee}' pattern='###,###,###,###,###,###,###'/>


GettingStarted
Tag Library Documentation
API Document

Reference : Head First Servlets & JSP



댓글