JSTL - fmt를 사용해서 데이터 format 변경하기.

Published on: 2009. 7. 29. 03:56 by louis.dev

jstl의 fmt를 사용하면 날자 데이터를 사용자가 원하는 표기법으로 표기한다던지(yyyy-mm-dd) 숫자 형식에서 소수점을 몇째짜리 까지 표현할 것인지를 정의 할 수 있다.

작업 순서
1. jsp 상단에 fmt 를 선언해 준다.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

2. 원하는 위치에 <fmt:~~ 이용해서 포맷을 변경한다.


다양한 fmt의 Attribute

1. fmt:formatNumber Attributes
Name Required Request-time Type Description
value false true java.lang.String Numeric value to be formatted.
type false true java.lang.String Specifies whether the value is to be formatted as number, currency, or percentage.
pattern false true java.lang.String Custom formatting pattern.
currencyCode false true java.lang.String ISO 4217 currency code. Applied only when formatting currencies (i.e. if type is equal to "currency"); ignored otherwise.
currencySymbol false true java.lang.String Currency symbol. Applied only when formatting currencies (i.e. if type is equal to "currency"); ignored otherwise.
groupingUsed false true java.lang.String Specifies whether the formatted output will contain any grouping separators.
maxIntegerDigits false true java.lang.String Maximum number of digits in the integer portion of the formatted output.
minIntegerDigits false true java.lang.String Minimum number of digits in the integer portion of the formatted output.
maxFractionDigits false true java.lang.String Maximum number of digits in the fractional portion of the formatted output.
minFractionDigits false true java.lang.String Minimum number of digits in the fractional portion of the formatted output.
var false false java.lang.String Name of the exported scoped variable which stores the formatted result as a String.
scope false false java.lang.String Scope of var.

 

2. fmt:parseNumber Attributes
Name Required Request-time Type Description
value false true java.lang.String String to be parsed.
type false true java.lang.String Specifies whether the string in the value attribute should be parsed as a number, currency, or percentage.
pattern false true java.lang.String Custom formatting pattern that determines how the string in the value attribute is to be parsed.
parseLocale false true java.lang.String Locale whose default formatting pattern (for numbers, currencies, or percentages, respectively) is to be used during the parse operation, or to which the pattern specified via the pattern attribute (if present) is applied.
integerOnly false true java.lang.String Specifies whether just the integer portion of the given value should be parsed.
var false false java.lang.String Name of the exported scoped variable which stores the parsed result (of type java.lang.Number).
scope false false java.lang.String Scope of var.

 

ex) 패턴 예제

  <fmt:formatNumber value="123456.78" pattern=",###"/> 3자리 기준으로 "," 찍기 (금액 / 소수점 제거)

  <fmt:formatNumber value="123456.78" pattern=".##"/> #의 갯수에 따라 소수점을 출력함 (반올림)

 

3. fmt:formatDate Attributes
Name Required Request-time Type Description
value true true java.lang.String Date and/or time to be formatted.
type false true java.lang.String Specifies whether the time, the date, or both the time and date components of the given date are to be formatted.
dateStyle false true java.lang.String Predefined formatting style for dates. Follows the semantics defined in class java.text.DateFormat. Applied only when formatting a date or both a date and time (i.e. if type is missing or is equal to "date" or "both"); ignored otherwise.
timeStyle false true java.lang.String Predefined formatting style for times. Follows the semantics defined in class java.text.DateFormat. Applied only when formatting a time or both a date and time (i.e. if type is equal to "time" or "both"); ignored otherwise.
pattern false true java.lang.String Custom formatting style for dates and times.
timeZone false true java.lang.String Time zone in which to represent the formatted time.
var false false java.lang.String Name of the exported scoped variable which stores the formatted result as a String.
scope false false java.lang.String Scope of var.

 

4. fmt:parseDate Attributes
Name Required Request-time Type Description
value false true java.lang.String Date string to be parsed.
type false true java.lang.String Specifies whether the date string in the value attribute is supposed to contain a time, a date, or both.
dateStyle false true java.lang.String Predefined formatting style for days which determines how the date component of the date string is to be parsed. Applied only when formatting a date or both a date and time (i.e. if type is missing or is equal to "date" or "both"); ignored otherwise.
timeStyle false true java.lang.String Predefined formatting styles for times which determines how the time component in the date string is to be parsed. Applied only when formatting a time or both a date and time (i.e. if type is equal to "time" or "both"); ignored otherwise.
pattern false true java.lang.String Custom formatting pattern which determines how the date string is to be parsed.
timeZone false true java.lang.String Time zone in which to interpret any time information in the date string.
parseLocale false true java.lang.String Locale whose predefined formatting styles for dates and times are to be used during the parse operation, or to which the pattern specified via the pattern attribute (if present) is applied.
var false false java.lang.String Name of the exported scoped variable in which the parsing result (of type java.util.Date) is stored.
scope false false java.lang.String Scope of var.

 

ex) 표기법 예제

  <fmt:formatDate value="timstamp" type="both" pattern="yy-MM-dd hh:mm"/> 날짜 및 시간

  <fmt:formatDate value="timstamp" type="date" pattern="yy-MM-dd"/> 날짜

  <fmt:formatDate value="timstamp" type="time" pattern="hh:mm"/> 시간


'Web > JSTL' 카테고리의 다른 글

JSTL - <c:import> , <c:redirect>  (0) 2009.04.15
JSTL - <c:url>  (0) 2009.04.15
JSTL - get 방식으로 넘어온 파라미터 값을 JSTL로 받기  (3) 2009.04.07
JSTL - 라이브러리 다운받기  (0) 2009.04.03