JSTL 정리 예제

    JSTL (JSP Standard Tag Library) : JSP 표준 태그 라이브러리

    -> JSP페이지를 작성할 때 유용한 여러 커스텀 액션과 함수가 있는 라이브러리이다.

    JSTL.jsp

    <%@page import="java.util.ArrayList"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> // jstl.jar 
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>jstl_test.jsp</title>
    </head>
    <body>
    <%-- 변수에 값 설정 --%>
    <c:set var="test" value="Hello"/>
    <%-- 변수에 설정된 값 출력 --%>
    <div>
    	 test : <c:out value="${test}"/>	 
    </div>
    
    <%-- 설정된 변수 제거 --%>
    <c:remove var="test"/>
    <div>
    	test : <c:out value="${test}"/>
    </div>
    
    <%-- if문 --%>
    <c:if test="${1 + 5 == 6 }">
    	결과 : <div>1 + 5는 6입니다</div>
    </c:if>
    
    <c:choose>
    	<c:when test="${1 + 5 == 50}">
    		<div>1 + 5는 50입니다.</div>
    	</c:when>
    	<c:when test="${1 + 5 == 30}">
    		<div>1 + 5는 30입니다.</div>
    	</c:when>
    	<c:otherwise>
    		<div>1 + 5는 50도 30도 아닙니다.</div>
    	</c:otherwise>
    </c:choose>
    
    <%-- for문 --%>
    <%-- for(int i =1; i <= 10; i+=2) --%>
    <c:forEach var="i" begin="1" end="10" step="2">
    	${i}<br>
    </c:forEach>
    <hr>
    
    <%
    	ArrayList<String> names = new ArrayList<>();
    	names.add("걸음마코드");
    	names.add("홍길동");
    	names.add("슈퍼맨");
    	names.add("아이언맨");
    	names.add("배트맨");
    	
    	request.setAttribute("names", names);
    %>	
    ${names}<br>
    <%-- for(String name : names) --%>
    <c:forEach items="${names}" var="name">
    	${name}<br>
    </c:forEach>
    </body>
    </html>

    'JSP' 카테고리의 다른 글

    EL 정리 예제 2  (0) 2023.09.12
    EL 정리 예제 1  (0) 2023.09.11
    세션(session) 개요  (0) 2021.01.16
    쿠키(Cookie) 예제  (0) 2021.01.16
    자바빈(JavaBean) 개요와 예제  (0) 2021.01.16

    댓글