웹찢남

Spring - JSTL 본문

BACK_END/Spring 공부

Spring - JSTL

harry595 2021. 2. 3. 18:19

JSTL은 JSP 페이지에서 조건문 처리, 반복문 처리 등을 html tag 형태로 작성할 수 있게 도와줌

사용법

1) tomcat.apache.org/download-taglibs.cgi

 

Apache Tomcat® - Apache Taglibs Downloads

Welcome to the Apache Taglibs download page. This page provides download links for obtaining the latest version of the Apache Standard Taglib, as well as links to the archives of older releases. You must verify the integrity of the downloaded files. We pro

tomcat.apache.org

위 링크에서 3가지 jar 파일을 다운 후 WEB-INF/lib 폴더에 복사

 

JSTL이 제공하는 태그의 종류

 

코어 태그

 

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

<c:set var="varName" scope="session" value="someValue" />

-> scope 에 해당하는 변수 set

<c:set var="varName" scope="request">

some Value

</c:set>

 

JSTL-set 사용 예시

<%@ 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="harry595" />
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

<h1>I'm ${value1}</h1>
<c:remove var="value1" scope="request" />
<h1>I'm ${value1}</h1>

</body>
</html>

 

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

<c:set targete="${some}" property="propertyName" value="anyValue" />

some 객체가 자바빈일 경우: some.setPropertyName(anyvalue);

some 객체가 맵일 경우: some.put(propertyName, anyValue);

target - <c:set>으로 지정한 변수 객체

property - 프로퍼티 이름

value - 새로 지정할 프로퍼티 값

 

코어 태그 - 흐름제어 태그 - if

<c:if test="조건">

...             // test의 조건이 true면

...             // 몸체 내용을 처리한다

</c:if>

 

JSTL - if 사용 예시

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

<%-- <%
	request.setAttribute("n",10);
%> --%>
<c:set var="n" scope="request" value="10" />
<!DOCTYPE html>
<html>
<head>
<meta 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>

 

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

<c:choose>

  <c:when test="조건1">

     ...

  </c:when>

  <c:when test="조건2">

    ...

  </c:when>

  <c:otherwise>

    ...

  </c:otherwise>

</c:choose>

 

JSTL - choose 사용 예시

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.*" %>
<%
    request.setAttribute("score", 83);
%>

<!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=UTF-8">
<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>

 

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

<c:forEach var="변수" items="아이템" [begin="시작번호"][end="끝번호"]>

...

${변수}

...

</c:forEach>

var - EL에서 사용될 변수명

items - 배열,List,Iterator,Enumeration,Map등의 Collection

begin - items에 지정한 목록에서 값을 읽어올 인덱스의 시작값

end - item에 지정한 목록에서 값을 읽어올 인덱스의 끝값

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.*" %>
<%
    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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${list}" var="item" begin="1" end="2">
${item } <br>
</c:forEach>
</body>
</html>

 

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

지정한 URL에 연결하여 결과를 지정한 변수에 저장한다.

<c:import url="URL" char Encoding="캐릭터 인코딩" var="변수명" scope="범위" >
  <c:param name="파라미터 이름" value="파라미터값" />

</c:import>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.*" %>
<c:import url="https://www.naver.com" var="urlValue" scope="request"></c:import>

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
읽어들인 값 : ${urlValue}
</body>
</html>

 

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

<c:redirect url="리다이렉트할 URL">

  <c:param name="파라미터 이름" value="파라미터 값'/>

</c:redirect>

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

 

코어 태그: 기타 태그 - out (JspWriter에 데이터를 출력한다)

<c:out value="value" escapeXml="{true|false}" default="defaultValue"/>

value: JspWriter에 출력할 값을 나타냄. 일반적으로 value의 속성 값은 String과 같은 문자열임

만약 value의 값이 java.io.Reader의 한 종류라면 out 태그는 Reader로 부터 데이터를 읽어와 JspWriter에 값을 출력

escapeXml: 이 속성의 값이 true면 문자를 변경 (<,> -> &lt;,&gt; 등 XSS를 방지할 수 있음)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 
<!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=UTF-8">
<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" />
<c:out value="${t}" escapeXml="false" />
</body>
</html>

 

'BACK_END > Spring 공부' 카테고리의 다른 글

Spring - JDBC  (0) 2021.02.15
Spring - Spring Core  (0) 2021.02.13
Spring - EL (Expression Language)  (0) 2021.02.02
Spring - Scope  (0) 2021.02.02
Spring - JSP  (0) 2021.02.01
Comments