07 JSTL(JSP 스탠다드 태그 라이브러리)
p160 - build.gradle
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.2.8'
id 'io.spring.dependency-management' version '1.1.6'
}
group = 'education'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api'
implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
implementation 'jakarta.servlet:jakarta.servlet-api'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
p161 - src/main/webapp/jsp/jstl/core-out.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String userName = "LEE";
request.setAttribute("userName", userName);
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>Welcome, <c:out value="${userName}" default="Guest" /></p>
</body>
</html>
p162 - src/main/webapp/jsp/jstl/core-set.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String userName = "LEE";
request.setAttribute("userName", userName);
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:set var="greeting" value="Hello, World!" scope="page" />
<%-- request 스코프에서 greeting 변수를 설정 --%>
<c:set var="greeting2" value="Hello, from Request Scope!" scope="request" />
<%-- session 스코프에서 greeting 변수를 설정 --%>
<c:set var="greeting3" value="Hello, from Session Scope!" scope="session" />
<p>Message: <c:out value="${greeting}" /></p>
<%-- application 스코프에서 greeting 변수를 설정 --%>
<c:set var="greeting4" value="Hello, from Application Scope!" scope="application" />
</body>
</html>
p164 - src/main/webapp/jsp/jstl/core-remove.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String userName = "LEE";
request.setAttribute("userName", userName);
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:set var="greeting" value="Hello, World!" scope="page" />
<c:remove var="greeting" />
<p>Message: <c:out value="${message}" default="No message found." /></p>
</body>
</html>
p165 - src/main/webapp/jsp/jstl/core-if.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>User Age Check</title>
</head>
<body>
<%-- 나이 변수를 설정 --%>
<c:set var="age" value="20" />
<%-- 조건에 따라 성인 여부를 출력 --%>
<c:if test="${age >= 18}">
<p>You are an adult.</p>
</c:if>
<c:if test="${age < 18}">
<p>You are not an adult.</p>
</c:if>
</body>
</html>
p166 - src/main/webapp/jsp/jstl/core-choose.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>User Age Check</title>
</head>
<body>
<%-- 나이 변수를 설정 --%>
<c:set var="role" value="user" />
<c:choose>
<c:when test="${role == 'admin'}">
<p>Welcome, Admin!</p>
</c:when>
<c:when test="${role == 'user'}">
<p>Welcome, User!</p>
</c:when>
<c:otherwise>
<p>Welcome, Guest!</p>
</c:otherwise>
</c:choose>
</body>
</html>
p168 - src/main/webapp/jsp/jstl/core-foreach.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>User Roles Check</title>
</head>
<body>
<%-- 역할 리스트 설정 --%>
<c:set var="roles" value="${fn:split('admin,user,guest', ',')}" />
<%-- 각 역할에 따른 메시지 출력 --%>
<c:forEach var="role" items="${roles}">
<c:choose>
<c:when test="${role == 'admin'}">
<p>Welcome, Admin!</p>
</c:when>
<c:when test="${role == 'user'}">
<p>Welcome, User!</p>
</c:when>
<c:otherwise>
<p>Welcome, Guest!</p>
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>
p169 - src/main/webapp/jsp/jstl/core-fortokens.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>User Age Check</title>
</head>
<body>
<c:forTokens items="Java,Python,JavaScript" delims="," var="language">
<p>${language}</p>
</c:forTokens>
</body>
</html>
p170 - src/main/webapp/jsp/jstl/core-import.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 헤더 포함 -->
<c:import url="header.jsp" />
<!-- 메인 콘텐츠 -->
<p>This is the main content of the page.</p>
<!-- 푸터 포함 -->
<c:import url="footer.jsp" />
p171 - src/main/webapp/jsp/jstl/core-url.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>User Age Check</title>
</head>
<body>
<%-- 나이 변수를 설정 --%>
<c:set var="id" value="3" />
<c:url value="/profile" var="profileUrl">
<c:param name="userId" value="${id}" />
</c:url>
<a href="${profileUrl}">View Profile</a>
</body>
</html>
p173 - src/main/webapp/jsp/jstl/fmt.jsp
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
java.util.Date now = new java.util.Date();
request.setAttribute("now", now);
%>
<%
double amount = 1234567.89;
request.setAttribute("amount", amount);
%>
<p>Current Date and Time:</p>
<!-- 날짜만 포맷 -->
<fmt:formatDate value="${now}" pattern="yyyy-MM-dd" />
<br>
<!-- 시간만 포맷 -->
<fmt:formatDate value="${now}" type="time" timeStyle="short" />
<br>
<!-- 날짜와 시간 모두 포맷 -->
<fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="short" />
<br>
<p>Original Number: ${amount}</p>
<!-- 숫자 포맷 -->
<p>Formatted Number: <fmt:formatNumber value="${amount}" /></p>
<!-- 통화 포맷 -->
<p>Formatted Currency: <fmt:formatNumber value="${amount}" type="currency" /></p>
<!-- 백분율 포맷 -->
<p>Formatted Percentage: <fmt:formatNumber value="0.85" type="percent" /></p>
<!-- 사용자 정의 패턴 포맷 -->
<p>Custom Pattern: <fmt:formatNumber value="${amount}" pattern="#,###.00" /></p>
<!-- 로케일을 미국으로 설정 -->
<fmt:setLocale value="en_US" />
<p>Current Locale is set to US</p>
<!-- 로케일을 한국으로 설정 -->
<fmt:setLocale value="ko_KR" />
<p>Current Locale is set to Korea</p>
p174, 176 - src/main/webapp/jsp/jstl/fmt-messages.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!-- 로케일을 한국어로 설정 -->
<%--<fmt:setLocale value="ko_KR" />--%>
<%--<fmt:setLocale value="en_US" />--%>
<%--<fmt:setLocale value="${header['Accept-Language']}" />--%>
<!-- 메시지 번들 설정 -->
<fmt:setBundle basename="messages" />
<!-- 로케일에 맞는 메시지 출력 -->
<p><fmt:message key="greeting" /></p>
<p><fmt:message key="farewell" /></p>
<p>Browser Accept-Language: ${header['Accept-Language']}</p>
p178 - src/main/webapp/jsp/jstl/fn.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!-- 문자열 길이 확인 -->
<p>Length of 'Hello World': ${fn:length('Hello World')}</p>
<!-- 문자열 포함 여부 확인 -->
<c:if test="${fn:contains('Hello World', 'World')}">
<p>'World' is found in 'Hello World'.</p>
</c:if>
<!-- 배열을 문자열로 결합 -->
<%
String[] words = {"Hello", "World", "JSTL"};
request.setAttribute("words", words);
%>
<p>Joined String: ${fn:join(words, ', ')}</p>
<!-- 부분 문자열 추출 -->
<p>Substring of 'Hello World': ${fn:substring('Hello World', 0, 5)}</p>
Last updated