JSP/SERVLET
2022.02.28 / 18:25

JSTL-if, if else 5ºÐ¾È¿¡ Á¤º¹Çϱâ

MyData
Ãßõ ¼ö 159

IF¹® : <c:if>

´Ü¼ø if¹®À» ±¸¼ºÇÒ¶§ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
<head>
<title><c:if> Tag Example</title>
</head>

<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:if test = "${salary > 2000}">
<p>My salary is: <c:out value = "${salary}"/><p>
</c:if>
</body>
</html>

À§ÀÇ °á°ú´Â ¾Æ·¡¿Í °°½À´Ï´Ù.

1
My salary is: 4000

IF ~ ELSE ¹® : <c:choose>

java¿¡¼­ ¸¹ÀÌ »ç¿ëÇÏ´Â if~else ¹®ÀÇ °æ¿ì jstl¿¡¼­´Â <c:choose>¸¦ ÀÌ¿ëÇÕ´Ï´Ù.

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
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
<head>
<title><c:choose> Tag Example</title>
</head>

<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<p>Your salary is : <c:out value = "${salary}"/></p>
<c:choose>

<c:when test = "${salary <= 0}">
Salary is very low to survive.
</c:when>

<c:when test = "${salary > 1000}">
Salary is very good.
</c:when>

<c:otherwise>
No comment sir...
</c:otherwise>
</c:choose>

</body>
</html>
1
2
Your salary is : 4000
Salary is very good.

ºñ±³±âÈ£ : eq, ne, empty, not empty

if¹®À» »ç¿ëÇÒ¶§¿¡´Â ¹Ýµå½Ã °ª°ú ºñ±³¸¦ Çؼ­ °á°ú¸¦ ¾ò±â¶§¹®¿¡, jstl¿¡¼­´Â eq, ne¿Í °°Àº ºñ±³±âÈ£¸¦ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.

  1. eq (==)
    µ¿ÀÏÇÑ °ªÀÎÁö È®ÀÎÇϱâ À§ÇØ »ç¿ëÇÕ´Ï´Ù.
1
2
3
4
5
6
7
8
9
10
11
<c:if test="${name == 'µÑ¸®'}">

<c:if test="${name eq 'µÑ¸®'}">

<c:if test="${name == null}">

<c:if test="${name eq null}">

<c:if test="${age == 7}">

<c:if test="${age eq 7}">
  1. ne (!=)
    µ¿ÀÏÇÏÁö ¾ÊÀº °ªÀ» È®ÀÎÇϱâ À§ÇØ »ç¿ëÇÕ´Ï´Ù.
1
2
3
4
5
6
7
<c:if test="${name != 'µÑ¸®'}">

<c:if test="${name ne 'µÑ¸®'}">

<c:if test="${age != 5}">

<c:if test="${age ne 5}">
  1. empty (== null)
    ºñ±³ÇÏ´Â °ªÀÌ null ÀÎÁö È®ÀÎÇÒ¶§ »ç¿ëÇÕ´Ï´Ù.
    nullÀÌ ¾Æ´Ñ°æ¿ì¸¦ Ç¥ÇöÇÒ¶§´Â !empty³ª not empty Ç¥ÇöÇÕ´Ï´Ù.
1
2
3
4
5
<c:if test="${empty name}">

<c:if test="${not empty name}">

<c:if test="${!empty name}">

³í¸® ¿¬»êÀÚ

  • and && : ¸ðµÎ ÂüÀ϶§ ÂüÀÌ µË´Ï´Ù.
1
2
3
<c:if test="${a > b and c < d}">

<c:if test="${a > b && c < d}">
  • or || : µÑÁß Çϳª¶óµµ ÂüÀ̸é ÂüÀÌ µË´Ï´Ù.
1
2
3
<c:if test="${a > b or c < d}">

<c:if test="${a > b || c < d}">
  • not ! : ³í¸®¸¦ ¹ÝÀüÇÕ´Ï´Ù. ÂüÀÌ¸é °ÅÁþÀ¸·Î º¯°æµÇ°í, °ÅÁþÀ̸é ÂüÀ¸·Î º¯È¯µË´Ï´Ù.
1
2
3
<c:if test="${not a == ''}">

<c:if test="${! a == ''}">

Reference