JSTL

jinkyung·2021년 1월 21일
0

JSP

목록 보기
10/20

JSTL

jsp에서 자바 코드 사용을 줄이기 위해 쓴다.
jstl 문법을 사용하기 위해 라이브러리를 추가했다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 태그 또한 코드 상단에 써줘야한다.


  • 출력
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p><a href="jstl02.jsp">[다음]</a></p>
	
	<h2>c:out 태그</h2>
	<!-- 둘다 있으면 value 값이 먼저 출력된다  -->
	1) <c:out value="안녕하세요"/><br>
	2) <c:out value="${null}">반갑습니다.</c:out><br>
	3) <c:out value="안녕하세요!">반갑습니다.</c:out><br>
	4) <c:out value="${null}"/><br>
	
	<p><a href="jstl02.jsp">[다음]</a></p>
</body>
</html>
  • 값 설정
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl01.jsp">[이전]</a>
		<a href="jstl03.jsp">[다음]</a>
	</p>
	
	<h2>c:set 태그</h2>
	<h3>값 설정 방식</h3>
	<!-- pageScope에 저장된다  -->
	<c:set var="username1" value="홍길동"/>
	<c:set var="username2">임꺽정</c:set>
	1) ${username1}<br>
	2) ${username2}<br>
	
	<h3>기본 보관소 - page</h3>
	3) ${pageScope.username1}<br>
	4) ${requestScope.username1}<br>	
	
	<h3>보관소 지정 - scope 속성</h3>
	<c:set var="username3" scope="request">일지매</c:set>
	5) ${pageScope.username3}<br>
	6) ${requestScope.username3}<br>
	
	<h3>기존의 값 덮어씀</h3>
	<% pageContext.setAttribute("username4", "똘이장군"); %>
	7) 기존 값=${username4}<br>
	<c:set var="username4" value="주먹대장"/>
	8) 덮어쓴 값=${username4}<br>
	
	<h3>객체의 프로퍼티 값 변경</h3>
	<%!
		public static class MyMember{
			int no;
			String name;
			
			public int getNo(){
				return no;
			}
			public void setNo(int no){
				this.no = no;
			}
			public String getName(){
				return name;
			}
			public void setName(String name){
				this.name = name;
			}
		}
	%>
	<%
		MyMember member = new MyMember();
		member.setNo(100);
		member.setName("홍길동");
		pageContext.setAttribute("member", member);
	%>
	9) ${member.name}<br>
	<c:set target="${member}" property="name" value="임꺽정"/>
	10) ${member.name}<br>	<!-- 임꺽정 출력  -->
	
	<p>
		<a href="jstl01.jsp">[이전]</a>
		<a href="jstl03.jsp">[다음]</a>
	</p>
</body>
</html>
  • 값 제거
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl02.jsp">[이전]</a>
		<a href="jstl04.jsp">[다음]</a>
	</p>
	
	<h3>c:remove 태그</h3>
	<h3>보관소에 저장된 값 제거</h3>
	<% pageContext.setAttribute("username1", "홍길동"); %>
	
	1) ${username1}<br>
	<c:remove var="username1"/>
	2) ${username1}<br>
	
	<p>
		<a href="jstl02.jsp">[이전]</a>
		<a href="jstl04.jsp">[다음]</a>
	</p>	
</body>
</html>
  • 조건문
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl03.jsp">[이전]</a>
		<a href="jstl05.jsp">[다음]</a>
	</p>
	
	<h2>c:if태그</h2>
	
	<!-- 조건문이 참이면 출력, 거짓이면 출력 X -->
	<c:if test="${10 > 20}" var="result1">
		1) 10은 20보다 크다.
	</c:if>
	2) ${result1}<br>
	
	<c:if test="${10 < 20}" var="result2">
		1) 20은 10보다 크다.<br>
	</c:if>
	2) ${result2}<br>	
	
	<p>
		<a href="jstl03.jsp">[이전]</a>
		<a href="jstl05.jsp">[다음]</a>
	</p>
</body>
</html>
  • switch case
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl04.jsp">[이전]</a>
		<a href="jstl06.jsp">[다음]</a>
	</p>
	
	<h2>c:choose 태그</h2>
	<c:set var="userid" value="admin"/>
	<c:choose>
		<c:when test="${userid=='hong'}">
			홍길동님 반갑습니다.
		</c:when>
		<c:when test="${userid=='lim'}">
			임꺽정님 반갑습니다.
		</c:when>
		<c:when test="${userid=='admin'}">
			관리자님 반갑습니다.
		</c:when>
		<c:otherwise>
			등록되지 않은 사용자입니다.
		</c:otherwise>
	</c:choose>
	
	<p>
		<a href="jstl04.jsp">[이전]</a>
		<a href="jstl06.jsp">[다음]</a>
	</p>
</body>
</html>
  • 반복문
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl05.jsp">[이전]</a>
		<a href="jstl07.jsp">[다음]</a>
	</p>
	
	<h2>c:forEach 태그</h2>
	
	<h3>반복문 - 배열</h3>
	<% pageContext.setAttribute("nameList", 
			new String[]{"홍길동", "임꺽정", "일지매"}); %>
	<ul>
		<c:forEach var="name" items="${nameList}">
			<li>${name}</li>
		</c:forEach>
	</ul>
	
	<h3>반복문 - 배열의 시작 인덱스와 종료 인덱스 지정</h3>
	<% pageContext.setAttribute("nameList2",
			new String[]{"홍길동","임꺽정","일지매","주먹대장","똘이장군"}); %>
	<ul>
		<c:forEach var="name" items="${nameList2}" begin="2" end="3">
			<li>${name}</li>
		</c:forEach>
	</ul>	
	
	<h3>반복문 - ArrayList 객체</h3>
	<%
		ArrayList<String> nameList3 = new ArrayList<String>();
		nameList3.add("홍길동");
		nameList3.add("임꺽정");
		nameList3.add("일지매");
		nameList3.add("주먹대장");
		nameList3.add("똘이장군");
		pageContext.setAttribute("nameList3", nameList3);
	%>
	<ul>
		<c:forEach var="name" items="${nameList3}">
			<li>${name}</li>
		</c:forEach>
	</ul>
	
	<h3>반복문 - 콤마로 구분된 문자열</h3>
	<% pageContext.setAttribute("nameList4", 
			"홍길동,임꺽정,일지매,주먹대장,똘이장군"); %>
	<ul>
		<c:forEach var="name" items="${nameList4}">
			<li>${name}</li>
		</c:forEach>
	</ul>
	
	<h3>반복문 - 특정 회수만큼 반복</h3>
	<ul>
		<c:forEach var="no" begin="1" end="6">
			<li>
				<a href="jstl0${no}.jsp">JSTL 예제 ${no}</a>
			</li>
		</c:forEach>
	</ul>
	
	<p>
		<a href="jstl05.jsp">[이전]</a>
		<a href="jstl07.jsp">[다음]</a>
	</p>
</body>
</html>
  • 구분자
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl06.jsp">[이전]</a>
		<a href="jstl08.jsp">[다음]</a>
	</p>
	
	<h2>c:forTokens 태그</h2>
	
	<% pageContext.setAttribute("tokens", 
			"v1=20&v2=30&op=+"); %>
	<ul>
		<c:forTokens var="item" items="${tokens}" delims="&">
			<li>${item}</li>
		</c:forTokens>
	</ul>
	
	<p>
		<a href="jstl06.jsp">[이전]</a>
		<a href="jstl08.jsp">[다음]</a>
	</p>
</body>
</html>
  • 페이지 이동
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl07.jsp">[이전]</a>
		<a href="jstl09.jsp">[다음]</a>
	</p>
	
	<h2>c:url 태그</h2>
	
	<c:url var="calcUrl" value="http://localhost:9999/_30_MVC_JSTL/calc/Calculator.jsp">
	<!-- get요청 인자 전달  -->
		<c:param name="v1" value="20"/>
		<c:param name="v2" value="30"/>
		<c:param name="op" value="+"/>
	</c:url>
	<a href="${calcUrl}">계산하기</a>
	
	<p>
		<a href="jstl07.jsp">[이전]</a>
		<a href="jstl09.jsp">[다음]</a>
	</p>

</body>
</html>


  • 정보 import
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl08.jsp">[이전]</a>
		<a href="jstl10.jsp">[다음]</a>
	</p>
	
	<h2>c:import 태그</h2>
	
	<h3>RSS 피드 가져오기</h3>
	<textarea rows="10" cols="80">
		<c:import url="http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108"/>
	</textarea>
	
	<h3>RSS 피드 가져오기 - 보관소에 저장</h3>
	<c:import var="zdnetRSS"
		url="http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108"/>
	<textarea rows="10" cols="80">
		${zdnetRSS}
	</textarea>
	
	<p>
		<a href="jstl08.jsp">[이전]</a>
		<a href="jstl10.jsp">[다음]</a>
	</p>
</body>
</html>
  • 리다이렉트 텍스트를 클릭하면 바로 다음사이트로 넘어간다.
    jstl10.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl09.jsp">[이전]</a>
		<a href="jstl11.jsp">[다음]</a>
	</p>
	
	<h2>c:redirect 태그</h2>
	<a href="jstl11.jsp">리다이렉트 테스트</a>
	
	<p>
		<a href="jstl09.jsp">[이전]</a>
		<a href="jstl11.jsp">[다음]</a>
	</p>
</body>
</html>

jstl11.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl09.jsp">[이전]</a>
		<a href="jstl11.jsp">[다음]</a>
	</p>
	
	<h2>c:redirect 태그</h2>
	<c:redirect url="http://www.daum.net"/>
	
	<p>
		<a href="jstl09.jsp">[이전]</a>
		<a href="jstl11.jsp">[다음]</a>
	</p>
</body>
</html>

  • 날짜 pattern
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
	<p>
		<a href="jstl10.jsp">[이전]</a>
	</p>
	
	<h2>날짜 다루기</h2>
	<h3>fmt:parseDate 태그</h3>
	<code>
	<!-- '<'를 그냥 주면 태그의 시작 부분과 구분이 안돼서 &lt로 주었다. -->
	&lt;fmt:parseDate var="date1" value="2020-04-16"
	pattern="yyyy-MM-dd"/>
	</code>
	
	<fmt:parseDate var="date1" value="2020-04-16"
		pattern="yyyy-MM-dd"/>
		
	<h3>fmt:formatDate 태그</h3>
	<fmt:formatDate value="${date1}" pattern="MM/dd/yy" />
	
	<p>
		<a href="jstl10.jsp">[이전]</a>
	</p>
</body>
</html>

0개의 댓글