JSTL標簽提供了對國際化(I18N)的支持,它可以根據(jù)發(fā)出請求的客戶端地域的不同來顯示不同的語言。同時還提供了格式化數(shù)據(jù)和日期的方法。實現(xiàn)這些功能需要I18N格式標簽庫(I18N-capable formation tags liberary)。引入該標簽庫的方法為:
%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
I18N格式標簽庫提供了11個標簽,這些 標簽從功能上可以劃分為3類如下:
(1)數(shù)字日期格式化。formatNumber標簽、formatData標簽、parseNumber標簽、parseDate標簽、timeZone標簽、setTimeZone標簽。
(2)讀取消息資源。bundle標簽、message標簽、setBundle標簽。
(3)國際化。setlocale標簽、requestEncoding標簽。
接下將詳細介紹這些標簽的功能和使用方式。
fmt:formatNumber>標簽
根據(jù)區(qū)域或定制的方式將數(shù)字格式化成數(shù)字、貨幣或百分比
fmt:formatNumber value="number" [type={number|currency|percent|}] [pattern="pattern"] [currencyCode="currencyCode"] [currentSymbol="currentSymbol"] [groupingUsec="{true|false}"] [maxIntergerDigits="maxIntergerDigits"] [minIntergerDigits="minIntergerDigits"] [maxFractionDigits="maxFractionDigits"] [minFractionDigits="minFractionDigits"] [var="varname"] [scope="page|request|session|application"] />
%@page language="java" contentType="text/html;charset=utf-8"%> %@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> !DOCTYPE html> html> head> title>FormatNumber標簽使用/title> /head> body> h1>FormatNumber標簽使用/h1> fmt:setLocale value="fr_fr" /> France:fmt:formatNumber value="123456789.012"/> fmt:setLocale value="zh_cn" /> China:fmt:formatNumber value="123456789.012"/> fmt:setLocale value="de_de" /> Germany:fmt:formatNumber value="123456789.012"/> /body> /html>
fmt:parseNumber />標簽
用來將字符串類型的數(shù)字、貨幣、或百分比轉換成數(shù)字類型
fmt:parseNumber value="numberString" [type={number|currency|percent|}] [pattern="pattern"] [parseLocale="parseLocale"] [integerOnly="{false|true}"] [var="varname"] [scope="page|request|session|application"] />
fmt:formatDate />標簽
用來將日期類型轉換為字符串類型日期
fmt:formatDate value="number" [type={time|date|both}] [pattern="pattern"] [dateStyle="{default|short|medium|long|full}"] [timeStyle="{default|short|medium|long|full}"] [timeZone="timeZone"] [var="varname"] [scope="page|request|session|application"] />
fmt:parseDate />標簽
用來將字符串類型的時間或日期轉換成日期時間類型
fmt:parseDate value="date" [type={time|date|both}] [pattern="pattern"] [dateStyle="{default|short|medium|long|full}"] [timeStyle="{default|short|medium|long|full}"] [timeZone="timeZone"] [var="varname"] [scope="page|request|session|application"] />
fmt:setTimeZone />標簽
用來設置默認時區(qū)或將時區(qū)存儲到屬性范圍中
fmt:setTimeZone value="timezone" [var="varname"] [scope="{page|request|session|application}"] />
fmt:timeZone value="timeZone"> 本體內容 /fmt:timeZone>
fmt:setLocale />標簽
用來設定用戶的區(qū)域語言
fmt:setLocale value="locale" [variant="variant"] [scope="{page|request|session|application}"] />
fmt:requestEncoding value="charsetName" />
fmt:setBundle />標簽
用來設定默認的數(shù)據(jù)來源,也可以將其存儲到一定范圍中,供需要時使用
fmt:setBundle basename="basename" [var="varname"] [scope="{page|request|session|application}"] />
fmt:message />標簽
用來從指定的資源文件中通過索引取得值
fmt:message key="messageKey" [bundle="resourceBundle"] [var="varname"] [scope="{page|request|session|application}"] />
fmt:param />標簽
用來傳遞參數(shù)(在從資源文件中取得信息時,可能需要動態(tài)設定參數(shù)的情況下)
fmt:param value="messageParameter" />
沒有本體內容
fmt:param value="messageParameter" >有本體內容 參數(shù) /fmt:param>
fmt:bundle />標簽
用來設定數(shù)據(jù)來源
fmt:bundle basename="basename" [prefix="prefix"] > 本體內容fmt:message> /fmt:bundle>