728x90
반응형
Java의 If, Else 문법은 JSTL에서 when과 otherwise를 사용한다.
1. IF문(단일) 사용하기
2. IF ELSE 사용하기
3. 다중 IF 사용하기
주의사항
JSP파일 상단에 C taglib 추가하기
1
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
1. 단일 IF문
1
2
3
4
|
<c:set var="index" value="0" />
<c:if test="${index eq 0 }">
index 값 : ${index }
</c:if>
|
2. If Else문 (<c:choose> <c:otherwise>)
주의사항 - <c:when> <c:otherwise>(if else)문 사용할 경우 <c:choose></c:choose>로 감싸 주기
1
2
3
4
5
6
7
8
9
|
<c:set var="index" value="0" />
<c:choose>
<c:when test="${index eq 1 }">
index 값은 1입니다.
</c:when>
<c:otherwise>
index 값 : ${index }
</c:otherwise>
</c:choose>
|
3. 다중 If문
eq(==) / ne(!=)
방법 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<c:set var="index" value="0" />
<c:set var="name" value="홍길동" />
<c:choose>
<c:when test="${index eq 0 }">
<c:if test="${name eq '홍길동' }">
name 변수 값은 홍길동입니다.
</c:if>
<c:if test="${name eq '홍길동1' }">
name 변수 값은 ${name }입니다.
</c:if>
</c:when>
<c:otherwise>
index 값 : ${index }
</c:otherwise>
</c:choose>
|
방법 2
주의사항 - <c:when><c:otherwise> 앞에 <c:choose></c:choose>태그로 감싸주기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<c:set var="index" value="0" />
<c:set var="name" value="홍길동1" />
<c:choose>
<c:when test="${index eq 0 }">
<c:choose>
<c:when test="${name eq '홍길동' }">
name 변수 값은 홍길동입니다.
</c:when>
<c:when test="${name eq '홍길동1' }">
name 변수 값은 홍길동1입니다.
</c:when>
<c:otherwise>
name 변수 값은 ${name }입니다.
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
index 값 : ${index }
</c:otherwise>
</c:choose>
|
728x90
반응형
'Web > JSP' 카테고리의 다른 글
[JSP] js, css import example (0) | 2019.07.07 |
---|---|
[JSP] JSTL forEach Example (0) | 2019.07.07 |
[JSP] JSTL Tag 사용 Error (0) | 2019.07.07 |