티스토리 뷰

JSTL이란?

  • JSTL(JSP Standard Tag Library)은 JSP 페이지에서 조건문 처리, 반복문 처리 등을 html tag형태로 작성할 수 있게 도와줍니다.

 

 

JSTL을 사용하려면?

 

 

 

 

JSTL이 제공하는 태그의 종류

 

 

 

코어 태그

 

 

 

 

코어 태그: 변수 지원 태그 - set, remove

 

 

*jstl01.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
 <!-- 라이브러리를 사용하기 위한 지시자, 커스텀 태그로 사용도 가능 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 
<c:set var="value1" scope="request" value="kwon"></c:set>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
성 : ${value1 }<br>
<c:remove var="value1" scope="request"/> <!-- 지우기 , 태그 사이에 아무것도 없다면 단일 태그로 가능.-->
성 : ${value1 }<br>
</body>
</html>
cs

 

 

 

 

코어태그: 변수 지원 태그 - 프로퍼티, 맵의 처리

 

 

 

 

 

코어 태그: 흐름제어 태그

 

 

 

*jstl02.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<%-- 아래 c:set 태그와 같은 뜻 <%
    request.setAttribute("n",10);
%> --%>
 
<c:set var="n" scope="request" value="10"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:if test="${n==0}">
    n은 0과 같습니다.
</c:if>
<c:if test="${n==10 }">
    n은 10과 같습니다.
</c:if>
</body>
</html>
cs

 

 

 

코어 태그: 흐름제어 태그 - choose

 

- if~else 문과 유사

 

 

 

*jstl03.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    request.setAttribute("score",83);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:choose>
    <c:when test="${score >= 90}">
        A학점 입니다.
    </c:when>
    <c:when test="${score >= 80 }">
        B학점 입니다.
    </c:when>
    <c:when test="${score>=70}">
        C학점 입니다.
    </c:when>
    <c:when test="${score>=60 }">
        D학점 입니다.
    </c:when>
    <c:otherwise>
        F학점 입니다.
    </c:otherwise>
</c:choose>
</body>
</html>
cs

 

 

코어 태그: 흐름제어 태그 - forEach

- 배열 및 컬렉션에 저장된 요소를 차례대로 처리한다.

- for문 처럼 특정 조건만큼만 반복을 하게 할 수 있다.

 

 

 

forEach 실습

- 리스트를 만들어 리스트를 requestScope에 넣고 해당 리스트의 값들을 forEach 태그로 출력하기

 

*jstl04.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 
<%
    List<String> list = new ArrayList<>();
    list.add("hello");
    list.add("world");
    list.add("!!!");
    
    request.setAttribute("list",list);
 
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${list}" var="item" begin="1">
    ${item }<br>
</c:forEach>
</body>
</html>
cs

 

 

 

코어 태그: 흐름제어태그 - import

 

- <c:param name="파라미터이름" value="파라미터값"/> ~ 쿼리문을 집어넣고 싶을때 사용하는 태그

 

  

 

*jstlValue.jsp

1
2
3
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
Kwon YoungGeun
cs

 

*jsp05.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 
<c:import url="http://localhost:8080/jstl/jstlValue.jsp" var="urlValue" scope="request"/> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    ${urlValue }
</body>
</html>
cs

 

 


코어 태그: 흐름제어태그 - redirect

- <c:param name = "파라미터 이름" value="파라미터 값">은 리다이렉트할 페이지에 전달해야 할 값들이 있을때 사용하면 된다.

 

 

*jstl06.jsp

1
2
3
4
5
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 
<c:redirect url="http://localhost:8080/jstl/jstl05.jsp"></c:redirect>
cs

 

 

코어 태그: 기타태그 - out

 

 

 

*jstl07.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:set var="t" value="<script type='text/javascript'>alert(1);</script>"/>
<%-- ${t } --%>
<c:out value="${t }" escapeXml="true"/>
</body>
</html>
cs


참고 사이트 : http://www.edwith.org/



본 게시물은 개인적인 용도로 작성된 게시물입니다. 이후 포트폴리오로 사용될 정리 자료이니 불펌과 무단도용은 하지 말아주시고 개인 공부 목적으로만 이용해주시기 바랍니다.

' > 부스트코스' 카테고리의 다른 글

[WEB]JavaScript 배열  (0) 2018.07.06
[WEB]Maven이란?  (0) 2018.06.29
[WEB]EL(Expression Language)  (0) 2018.06.28
[WEB]scope  (0) 2018.06.27
[WEB] servlet & jsp연동  (0) 2018.06.27
댓글