JSTL

ybong·2022년 2월 26일
0

JSTL을 이용하면 태그형식으로 조건문, 반복문 등을 사용할 수 있다. EL과 연동하면 더욱더 강력하게 사용이 가능하다. JSTL에 대해 알아보자.

JSTL이란?

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

https://cphinf.pstatic.net/mooc/20180130_149/1517289583487Ac0YJ_PNG/2_6_2_jstl.PNG

JSTL이 제공하는 태그의 종류

https://cphinf.pstatic.net/mooc/20180130_273/1517290494334HrB7S_PNG/2_6_2_jstl___.PNG

  • 가장 중요한 것이 코어태그 기능

JSTL 환경설정

코어태그

https://cphinf.pstatic.net/mooc/20180130_226/1517290578353rKRbE_PNG/2_6_2_jstl_.PNG

  • 접두어 c:를 붙여 사용한다.
  • 논리적인 흐름을 태그로 처리할 수 있도록 도와준다.
  • URL의 내용을 읽어오거나 출력하는 기능도 포함되어있다.

변수지원 태그 - set, remove

  • scope에 값을 저장/삭제한다.

https://cphinf.pstatic.net/mooc/20180226_240/1519633482313pWfP8_PNG/1.png

실습

  • JSTL 사용한다는 것을 지정해줘야 한다.
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  • 예제
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="value1" scope="request" value="kang"></c:set>
<html>
<head>
    <title>Title</title>
</head>
<body>: ${requestScope.value1} <br>
<c:remove var="value1" scope="request"/> <!--단일태그 가능-->: ${requestScope.value1} <br>
</body>
</html>
: kang
성 :
  • remove했기때문에 값이 나오지 않는다.

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

  • 프로퍼티란?
    • 객체의 값을 변경하거나 값을 읽어들이기 위한 getter setter 메서드를 생각하면 됨.
  • set 태그로 특정 메서드에 인자를 전달할 수도 있다.

https://cphinf.pstatic.net/mooc/20180226_103/1519633640114VKW2d_PNG/2.png

  • 위 태그를 통해 아래의 메서드가 실행될 것이다.

흐름제어 태그 - if

  • <c:if 조건> 로 사용

https://cphinf.pstatic.net/mooc/20180226_83/1519633710402BlJ2W_PNG/3.png

실습

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%--<%--%>
<%--    request.setAttribute("n", 10);--%>
<%--%>--%>
<c:set var="n" scope="request" value="10"/>
<!-- 위에 둘 다 같은 코드이다-->

<html>
<head>
    <title>Title</title>
</head>
<body>
<c:if test="${n == 0}">
    n과 0은 같습니다.
</c:if>
<c:if test="${n == 10}">
    n과 10은 같습니다.
</c:if>
</body>
</html>
n과 10은 같습니다.
  • 충족된 조건문만 실행된다.

흐름제어 태그 - choose

https://cphinf.pstatic.net/mooc/20180130_4/1517292532220uxSVD_PNG/2_6_2__choose.PNG

  • if - else 구문과 유사하다.

실습

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
    request.setAttribute("score", 83);
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:choose>
    <c:when test="${90 <= score}">
        A학점입니다.
    </c:when>
    <c:when test="${80 <= score}">
        B학점입니다.
    </c:when>
    <c:when test="${70 <= score}">
        C학점입니다.
    </c:when>
    <c:when test="${60 <= score}">
        D학점입니다.
    </c:when>
    <c:otherwise>
        F학점입니다.
    </c:otherwise>

</c:choose>
</body>
</html>
B학점입니다

흐름제어 태그 - forEach

  • 배열 및 Collection에 저장된 요소를 차례대로 뽑아올 수 있다.
  • for문처럼 시작과 끝만큼 반복하게 만들 수도 있다.

https://cphinf.pstatic.net/mooc/20180130_218/1517292735244tmWgM_PNG/2_6_2__forEach.PNG

  • items에서 [begin부터 end까지] 한바퀴 돌면서 변수 var에 넣는다.
    • [] - 생략가능

실습

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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);
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:forEach items="${list}" begin="1" var="item">
    ${item}<br>
</c:forEach>
</body>
</html>

흐름제어 태그 - import

  • 특정한 URL 페이지의 결과를 읽어들여 변수에 저장한다.

https://cphinf.pstatic.net/mooc/20180130_93/1517293018908uGgzT_PNG/2_6_2__import.PNG

  • <c:param> 태그는 URL 뒤에 쿼리문까지 읽어오고 싶을 때 붙인다.

실습

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:import url="http://localhost:8080/firstweb_war/jstlValue.jsp" var="urlValue" scope="request"/>
<c:import url="https://google.co.kr" var="google" scope="request"/>
<html>
<head>
    <title>Title</title>
</head>
<body>
${urlValue}
${google}
</body>
</html>

흐름제어 태그 - redirect

https://cphinf.pstatic.net/mooc/20180130_170/1517293246119dFJ4F_PNG/2_6_2__redirect.PNG

실습

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:redirect url="http://localhost:8080/firstweb_war/jstl05.jsp"/>
  • 리다이렉션 되어 해당 페이지를 보여준다.

기타 태그 - out

https://cphinf.pstatic.net/mooc/20180130_55/1517293404340WP4J3_PNG/2_6_2__out.PNG

  • 특정한 문자열을 출력할 수 있다.
  • value : 실제 writer에 출력할 값 (문자열)
  • escapeXml : 기본값 true
  • default : value값이 존재하지 않을 경우 기본값을 지정한다
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>Title</title>
</head>
<body>
<c:set var="t" value="<script type='text/javascript'>alert(1);</script>"/>

<c:out value="${t}" escapeXml="true"/> //자바스크립트 해석하지 않고 그냥 문자열로 뜬다.
${t} //팝업으로 뜬다
</body>
</html>
profile
study blog

0개의 댓글

관련 채용 정보