JSTL Formatting Tags
JSP의 시작 부분에 taglib 정의를 추가한다.
<fmt:bundle> Tag
resource bundle을 불러온다.
basename 속성에 지정된 properties 파일을 locale 에 맞는 것을 불러온다.
properties 파일은 패키지 형식의 이름을 가진다.
com\study\messages 에 messages.properties, messages_ko.properties, messages_en.properties 이 있다면
basename="com.study.messages.messages" 이라고 지정하면 된다.
locale이 ko 이라면 messages_ko.properties 파일을 불러오며, 해당되는 것이 없다면 message.properties 파일을 불러올 것이다.
<fmt:bundle basename="로드 할 번들 베이스 이름"
prefix="key명칭의 공통적인 부분" />
<fmt:message> Tag
메시지 키와 지역화된 메시지를 바꾸어 출력한다.
<fmt:message key="메시지 키"
bundle="사용할 번들" />
<fmt:setLocale> Tag
locale 변경
<fmt:setLocale value="locale" />
예제)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:requestEncoding value="EUC-KR" />
<!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=EUC-KR">
<title>JSTL Test 2</title>
</head>
<body>
디폴트 로케일은 <%= response.getLocale() %>입니다. <br/><br/>
<fmt:bundle basename="com.study.messages.messages" prefix="test.">
<fmt:message key="name"/>
</fmt:bundle>: <input type="text" name="name"/><br/>
<fmt:bundle basename="com.study.messages.messages" prefix="test.">
<fmt:message key="addr"/>
</fmt:bundle>: <input type="text" name="addr"/><br/><br/>
로케일을 en으로 변경합니다. <fmt:setLocale value="en" /><br/><br/>
<fmt:bundle basename="com.study.messages.messages">
<fmt:message key="test.name"/>
</fmt:bundle>: <input type="text" name="name"/><br/>
<fmt:bundle basename="com.study.messages.messages">
<fmt:message key="test.addr"/>
</fmt:bundle>: <input type="text" name="addr"/><br/>
</body>
</html>
<fmt:setBundle> Tag
페이지 전체에 사용할 수 있는 번들을 지정
<fmt:setBundle basename="로드 할 번들 베이스 이름"
var="번들을 저장하는 변수명" />
예제)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<fmt:requestEncoding value="EUC-KR" />
<fmt:setBundle basename="com.study.messages.messages" var="lc_msg" />
<!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=EUC-KR">
<title>JSTL Test 3</title>
</head>
<body>
<fmt:message key="test.name" bundle="${lc_msg}" />: <input type="text" name="name" /> <br />
<fmt:message key="test.addr" bundle="${lc_msg}" />: <input type="text" name="addr" /> <br />
</body>
</html>
<fmt:formatDate> Tag
날짜 형식을 표현
<fmt:formatDate value="날짜값"
[type="{time|date|both}"]
[dateStyle="{default|short|medium|long|full}"]
[timeStyle="{default|short|medium|long|full}"]
[pattern="customPattern"]
[timeZone="timeZone"]
[var="varName"]
[scope="{page|request|session|application}"] />
예제)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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=EUC-KR">
<title>JSTL Test4</title>
</head>
<body>
<jsp:useBean id="now" class="java.util.Date" />
<p>
Date in Current Zone:
<fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />
</p>
<p>TimeZone을 GMT-8 로 변경</p>
<fmt:setTimeZone value="GMT-8" />
<p>
Date in Changed Zone:
<fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />
</p>
<hr/>
<h2>Number Format:</h2>
Formatted Date (1): <fmt:formatDate value="${now}" type="time" /><br/>
Formatted Date (2): <fmt:formatDate value="${now}" type="date" /><br/>
Formatted Date (3): <fmt:formatDate value="${now}" type="both" /><br/>
Formatted Date (4): <fmt:formatDate value="${now}" type="both" dateStyle="short" timeStyle="short" /><br/>
Formatted Date (5): <fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="medium" /><br/>
Formatted Date (6): <fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long" /><br/>
Formatted Date (7): <fmt:formatDate value="${now}" pattern="yyyy-MM-dd" /><br/><br/>
<hr/><br/>
<table border="1" width="100%">
<tr>
<td width="100%" colspan="2" bgcolor="#0000FF">
<p align="center">
<b>
<font color="#FFFFFF" size="4" >
Formatting: <fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />
</font>
</b>
</p>
</td>
</tr>
<c:forEach var="zone" items="<%=java.util.TimeZone.getAvailableIDs()%>">
<tr>
<td width="51%">
<c:out value="${zone}" />
</td>
<td width="49%">
<fmt:timeZone value="${zone}">
<fmt:formatDate value="${now}" timeZone="${zn}" type="both" />
</fmt:timeZone>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<fmt:formatNumber> Tag
숫자 형식을 표현
<fmt:formatNumber value="수치값"
[type="{number|currency|percent}"]
[pattern="customPattern"]
[currencyCode="currencyCode"]
[currencySymbol="currencySymbol"]
[groupingUsed="{true|false}"]
[maxIntegerDigits="maxIntegerDigits"]
[minIntegerDigits="minIntegerDigits"]
[maxFractionDigits="maxFractionDigits"]
[minFractionDigits="minFractionDigits"]
[var="varName"]
[scope="{page|request|session|application}"] />
예제)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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=EUC-KR">
<title>JSTL Test5</title>
</head>
<body>
<h2>Number Format:</h2>
<c:set var="balance" value="120000.2309" />
Formatted Number (1): <fmt:formatNumber value="${balance}" type="currency" /><br/>
Formatted Number (2): <fmt:formatNumber value="${balance}" type="number" maxIntegerDigits="3" /><br/>
Formatted Number (3): <fmt:formatNumber value="${balance}" type="number" maxFractionDigits="3" /><br/>
Formatted Number (4): <fmt:formatNumber value="${balance}" type="number" groupingUsed="false" /><br/>
Formatted Number (5): <fmt:formatNumber value="${balance}" type="percent" maxIntegerDigits="3" /><br/>
Formatted Number (6): <fmt:formatNumber value="${balance}" type="percent" minFractionDigits="10" /><br/>
Formatted Number (7): <fmt:formatNumber value="${balance}" type="percent" maxIntegerDigits="3" /><br/>
Formatted Number (8): <fmt:formatNumber value="${balance}" type="number" pattern="###.###E0" /><br/>
<p>
Currency in USA :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/>
</p>
</body>
</html>
<fmt:parseDate> Tag
정해진 패턴의 문자열에서 날짜를 파싱
<fmt:parseDate value="dateString"
[type="{time|date|both}"]
[dateStyle="{default|short|medium|long|full}"]
[timeStyle="{default|short|medium|long|full}"]
[pattern="customPattern"]
[timeZone="timeZone"]
[parseLocale="parseLocale"]
[var="varName"]
[scope="{page|request|session|application}"] />
<fmt:parseNumber> Tag
정해진 패턴의 문자열에서 수치를 파싱
<fmt:parseNumber value="numericValue"
[type="{number|currency|percent}"]
[pattern="customPattern"]
[parseLocale="parseLocale"]
[integerOnly="{true|false}"]
[var="varName"]
[scope="{page|request|session|application}"] />
예제)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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=EUC-KR">
<title>JSTL Test6</title>
</head>
<body>
<h2>Date Parsing:</h2>
<c:set var="now" value="20-10-2010" />
<fmt:parseDate value="${now}" var="parsedEmpDate" pattern="dd-MM-yyyy" />
Parsed Date: <c:out value="${parsedEmpDate}" />
<hr/><br/>
<h2>Number Parsing:</h2>
<c:set var="balance" value="1250003.350" />
<fmt:parseNumber var="i" type="number" value="${balance}" />
Parsed Number (1) : <c:out value="${i}" /> <br/>
<fmt:parseNumber var="i" integerOnly="true" type="number" value="${balance}" />
Parsed Number (2) : <c:out value="${i}" /> <br/>
</body>
</html>