λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Spring/Boost Course Web

2.6 JSTL & EL

1) EL(Expression Language)

ν‘œν˜„ μ–Έμ–΄μ˜ κΈ°λ³Έ 객체 μ‚¬μš© 예

ν‘œν˜„ μ–Έμ–΄μ˜ 데이터 νƒ€μž…

  • λΆˆλ¦¬μ–Έ νƒ€μž… - true와 false

  • μ •μˆ˜νƒ€μž… - 0~9둜 이루어진 μ •μˆ˜ κ°’ 음수의 경우 '-'κ°€ λΆ™μŒ

  • μ‹€μˆ˜νƒ€μž… - 0~9둜 이루어져 있으며, μ†Œμˆ˜μ ('.')을 μ‚¬μš©ν•  수 있고, 3.24e3κ³Ό 같이 μ§€μˆ˜ν˜•μœΌλ‘œ ν‘œν˜„ κ°€λŠ₯ν•˜λ‹€.

  • λ¬Έμžμ—΄ νƒ€μž… - λ”°μ˜΄ν‘œ( ' λ˜λŠ”" )둜 λ‘˜λŸ¬μ‹Ό λ¬Έμžμ—΄. λ§Œμ•½ μž‘μ€ λ”°μ˜΄ν‘œ(')λ₯Ό μ‚¬μš©ν•΄μ„œ ν‘œν˜„ν•  경우 값에 ν¬ν•¨λœ μž‘μ€ λ”°μ˜΄ν‘œλŠ” \' 와 같이 \ κΈ°ν˜Έμ™€ ν•¨κ»˜ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

  • \ 기호 μžμ²΄λŠ” \\ 둜 ν‘œμ‹œν•œλ‹€.

  • 널 νƒ€μž… - null

 

 

객체 μ ‘κ·Ό κ·œμΉ™

  • ν‘œν˜„ 1μ΄λ‚˜ ν‘œν˜„ 2κ°€ null이면 null을 λ°˜ν™˜ν•œλ‹€.

  • ν‘œν˜„1이 Map일 경우 ν‘œν˜„2λ₯Ό keyλ‘œν•œ 값을 λ°˜ν™˜ν•œλ‹€.

  • ν‘œν˜„1이 Listλ‚˜ 배열이면 ν‘œν˜„2κ°€ μ •μˆ˜μΌ 경우 ν•΄λ‹Ή μ •μˆ˜ 번째 index에 ν•΄λ‹Ήν•˜λŠ” 값을 λ°˜ν™˜ν•œλ‹€. λ§Œμ•½ μ •μˆ˜κ°€ 아닐 κ²½μš°μ—λŠ” 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

  • ν‘œν˜„1이 객체일 κ²½μš°λŠ” ν‘œν˜„2에 ν•΄λ‹Ήν•˜λŠ” getterλ©”μ†Œλ“œμ— ν•΄λ‹Ήν•˜λŠ” λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•œ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•œλ‹€.

 

ν‘œν˜„ μ–Έμ–΄μ˜ 수치 μ—°μ‚°μž

  • + : λ§μ…ˆ

  • - : λΊ„μ…ˆ

  • * : κ³±μ…ˆ

  • / λ˜λŠ” div : λ‚˜λˆ—μ…ˆ

  • % λ˜λŠ” mod : λ‚˜λ¨Έμ§€

  • μˆ«μžκ°€ μ•„λ‹Œ 객체와 수치 μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•  경우 객체λ₯Ό 숫자 κ°’μœΌλ‘œ λ³€ν™˜ ν›„ μ—°μ‚°μžλ₯Ό μˆ˜ν–‰ : ${"10"+1} → ${10+1}

  • 숫자둜 λ³€ν™˜ν•  수 μ—†λŠ” 객체와 수치 μ—°μ‚°μžλ₯Ό ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ μ—λŸ¬λ₯Ό λ°œμƒ : ${"μ—΄"+1} → μ—λŸ¬

  • 수치 μ—°μ‚°μžμ—μ„œ μ‚¬μš©λ˜λŠ” 객체가 null이면 0으둜 처리 : ${null + 1} → ${0+1}

 

비ꡐ μ—°μ‚°μž

  • == λ˜λŠ” eq

  • != λ˜λŠ” ne

  • < λ˜λŠ” lt

  • > λ˜λŠ” gt

  • <= λ˜λŠ” le

  • >= λ˜λŠ” ge

  • λ¬Έμžμ—΄ 비ꡐ: ${str == 'κ°’'} str.compareTo("κ°’") == 0 κ³Ό 동일

 

논리 μ—°μ‚°μž

  • && λ˜λŠ” and

  • || λ˜λŠ” or

  • ! λ˜λŠ” not

 

empty μ—°μ‚°μž, 비ꡐ선택 μ—°μ‚°μž

 

μ—°μ‚°μž μš°μ„ μˆœμœ„

  1. [] .

  2. ()

  3. - (단일) not ! empty

  4. * / div % mod

  5. + -

  6. < > <= >= lt gt le ge

  7. == != eq ne

  8. && and

  9. || or

  10. ? :

 

ν‘œν˜„ μ–Έμ–΄ λΉ„ν™œμ„±ν™” : JSP에 λͺ…μ‹œν•˜κΈ°

default : false  EL 인식

  • <%@ page isELIgnored = "true" %>


el01.jsp  Scope별 ν‘œν˜„ μ–Έμ–΄μ˜ μ‚¬μš©

<%@ page language="java" contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

  <%

   pageContext.setAttribute("p1", "page scope value");

   request.setAttribute("r1", "request scope value");

   session.setAttribute("s1", "session scope value");

   application.setAttribute("a1", "application scope value");

%>



    <!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>

pageContext.getAttribute("p1") : ${pageScope.p1 }<br>

request.getAttribute("r1") : ${requestScope.r1 }<br>

session.getAttribute("s1") : ${sessionScope.s1 }<br>

application.getAttribute("a1") : ${applicationScope.a1 }<br>

<br><br>

λ³€μˆ˜κ°€ κ²ΉμΉ˜μ§€ μ•ŠμœΌλ‹ˆκΉŒ μ΄λ ‡κ²Œ μ‚¬μš© κ°€λŠ₯! _ ν•˜μ§€λ§Œ ν™•μ‹€νžˆ μ“°λŠ”κ²Œ 더 효율적

pageContext.getAttribute("p1") : ${p1 }<br>

request.getAttribute("r1") : ${r1 }<br>

session.getAttribute("s1") : ${s1 }<br>

application.getAttribute("a1") : ${a1 }<br>



</body>

</html>

el02.jsp  ν‘œν˜„ μ–Έμ–΄μ˜ μ—°μ‚°

<%@ page language="java" contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

<%

   request.setAttribute("k", 10);

   request.setAttribute("m", true);

%>   

<!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>

k : ${k } <br>

k + 5 : ${ k + 5 } <br>

k - 5 : ${ k - 5 } <br>

k * 5 : ${ k * 5 } <br>

k / 5 : ${ k div 5 } <br>





k : ${k }<br>

m : ${m }<br>

k > 5 : ${ k > 5 } <br>

k < 5 : ${ k < 5 } <br>

k <= 10 : ${ k <= 10} <br>

k >= 10 : ${ k >= 10 } <br>

m : ${ m } <br>

!m : ${ !m } <br>



</body>

</html>

 

* EL 문법 μ‚¬μš©ν•˜μ§€ μ•Šκ³  λ¬Έμžμ—΄ κ·ΈλŒ€λ‘œ ν‘œμ‹œν•  λ•Œ 

<%@ page isELIgnored = "true" %>

 


2) JSTL(JSP Standard Tag Library)

 

JSTLμ΄λž€?

  • JSTL(JSP Standard Tag Library)은 JSP νŽ˜μ΄μ§€μ—μ„œ 쑰건문 처리, 반볡문 처리 등을 html tagν˜•νƒœλ‘œ μž‘μ„±ν•  수 있게 λ„μ™€μ€λ‹ˆλ‹€.

 

JSTL을 μ‚¬μš©ν•˜λ €λ©΄?

  • http://tomcat.apache.org/download-taglibs.cgi
  • μœ„μ˜ μ‚¬μ΄νŠΈμ—μ„œ 3가지 jarνŒŒμΌμ„ λ‹€μš΄λ‘œλ“œ ν•œ ν›„ WEB-INF/lib/ 폴더에 볡사λ₯Ό ν•œλ‹€.

 

JSTL이 μ œκ³΅ν•˜λŠ” νƒœκ·Έμ˜ μ’…λ₯˜


μ½”μ–΄ νƒœκ·Έ


μ½”μ–΄ νƒœκ·Έ: λ³€μˆ˜ 지원 νƒœκ·Έ - set, remove


jstl01.jsp  set, remove의 ν™œμš©

<%@ page language="java" contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:set var="value1" scope="request" value="kang"/>



<!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>



μ„± : ${value1} <br>



<c:remove var="value1" scope="request"/>



μ„± : ${value1 }

</body>

</html>


μ½”μ–΄νƒœκ·Έ: λ³€μˆ˜ 지원 νƒœκ·Έ - ν”„λ‘œνΌν‹°, 맡의 처리



λ³€μˆ˜ 지원 νƒœκ·Έ

μ½”μ–΄ νƒœκ·Έ: νλ¦„μ œμ–΄ νƒœκ·Έ


jstl02.jsp  if의 ν™œμš©

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

request.setAttribute("n", 10);

%>



<!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:if test="${n == 0}">

n은 κ³Ό 0κ³Ό κ°™μŠ΅λ‹ˆλ‹€.

</c:if>



<c:if test="${n == 10}">

n은 κ³Ό 10κ³Ό κ°™μŠ΅λ‹ˆλ‹€.

</c:if>

</body>

</html>

μ½”μ–΄ νƒœκ·Έ: νλ¦„μ œμ–΄ νƒœκ·Έ - choose


jstl03.jsp  switch의 ν™œμš©

<%@ 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

jstl04.jsp  forEach의 ν™œμš©

<%@ 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">

${item } <br>

</c:forEach>

</body>

</html>


μ½”μ–΄ νƒœκ·Έ: νλ¦„μ œμ–΄νƒœκ·Έ - import

 

jstlValue.jsp  import의 ν™œμš©

<%@ page language="java" contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

Kang kyungmi


jstl05.jsp  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="http://localhost:8080/webapp/jstlValue.jsp" 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

 

jtl06.jsp  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="jstlRedirectPage.jsp"></c:redirect>





jstlRedirectPage.jsp  redirect의 ν™œμš©

<%@ page language="java" contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

<!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>

<h1> redirect된 ν™”λ©΄μž…λ‹ˆλ‹€.</h1>

</body>

</html>

μ½”μ–΄ νƒœκ·Έ: κΈ°νƒ€νƒœκ·Έ - out

jstl07.jsp  out의 ν™œμš©

<%@ 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>

'Spring > Boost Course Web' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

3.3 Spring JDBC  (0) 2020.11.19
3.2 Spring Test  (0) 2020.11.19
3.1 Spring Core  (0) 2020.11.19
2.5 Scope  (0) 2020.11.19
2.4 JSP  (0) 2020.11.19
2.3 Servlet  (0) 2020.11.19