티스토리 뷰

JSTL

[JSTL] Core :: choose tag

A형개발자 2016. 8. 8. 21:06

JSTL Core - Choose tag

여러 조건중에 해당하는 조건에 맞는 처리를 할 수 있다. when 태그를 사용하여 현재의 조건에 부합하는지 여부를 확인하고, 최종적으로는 otherwise를 통해서 Default 처리를 할 수도 있다. 자바에에서는 CASE 와 같은 동작을 한다고 생각하면 이해하기 쉬울 것 같다.

Syntax

<c:choose>
	<c:when test="${condition == '1'}">
	...
	</c:when>
	<c:when test="${condition == '2'}">
	...
	</c:when>
	<c:when test="${condition == '3'}">
	...
	</c:when>
	<c:otherwise>
	...
	</c:otherwise>
</c:choose>

How to use

<%@ page import="java.util.Calendar"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
	Calendar currentDate = Calendar.getInstance();
	int hour = currentDate.get(Calendar.HOUR_OF_DAY);
	request.setAttribute("hour", hour);
%>
<html>
<head>
	<title>JSTL choose tag example</title>
</head>
<body>
<h1>Hello world!</h1>
<c:choose>
	<c:when test="${hour >= 0 && hour < 6}">
		<P>Body Content => Time is <%=currentDate.getTime()%>. I am on the bed.</P>
	</c:when>
	<c:when test="${hour >= 6 && hour < 9}">
		<P>Body Content => Time is <%=currentDate.getTime()%>. I am ready to go the office.</P>
	</c:when>
	<c:when test="${hour >= 9 && hour < 18}">
		<P>Body Content => The hour on the my office is <%=currentDate.getTime()%>. I am working.</P>
	</c:when>	
	<c:otherwise>
		<P>Body Content => The hour on the my office is <%=currentDate.getTime()%>. Let's go to a pub.</P>
	</c:otherwise>	
</c:choose>
</body>
</html>  

Description

choose tag 또한 otherwise를 꼭 가지고 있을 필요는 없다. when 와 if 로 실수로 사용하지 않도록 주의 한다.


주의!

<c:when>
when 는 꼭 choose 태그 안에만 있어야 한다.
when 는 otherwise 태그 전에만 있어야 한다.
<c:otherwise>
otherwise 는 꼭 choose 태그 안에만 있어야 한다.
otherwise 는 choose 태그 내에서 마지막 위치에 있어야 한다. otherwise 다음엔 새로운 when(조건확인) 나오면 안된다.

Result

 

Reference


'JSTL' 카테고리의 다른 글

[JSTL] Core :: forTokens 태그  (0) 2016.08.10
[JSTL] Core :: forEach 태그  (0) 2016.08.10
[JSTL] Core - if tag  (0) 2016.08.07
[JSTL] Core :: catch tag  (0) 2016.08.07
[JSTL] Core :: remove tag  (0) 2016.08.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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 28
29 30
글 보관함