==========================================================================
一:JSTL格式化標籤又稱爲I18N標籤庫,主要用來編寫國際化的WEB應用,使用此功能可以對一個特定的語言請求做出合適的處理。
例如:中國內地用戶將顯示簡體中文,臺灣地區則顯示繁體中文,使用I18N格式化標籤庫還可以格式化數字和日期,例如同一數字或日趨,在不同國家可能有不同的格式,使用I18N格式標籤庫可以將數字和日期格式爲當地的格式。
在JSP頁面中要使用到格式化標籤,需要引入下面的語句:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"% >
二:概覽
格式化標籤
<fmt:fromatNumber>
<fmt:formatDate>
<fmt:parseDate>
<fmt:parseNumber>
<fmt:setTimeZone>
<fmt:timeZone>
國際化標籤
<fmt:setLocale>
<fmt:requestEncoding>
<fmt:bundle>
<fmt:message>
<fmt:param>
<fmt:setBundle>
三:<fmt:formatNumber>
此標籤會根據區域定製的方式將數字格式化成數字,貨幣,百分比。
此標籤的屬性:
value:要格式化的數字
type:按照什麼類型格式化
pattern:自定義格式化樣式
currencyCode:ISO-4721貨幣代碼,只適用於按照貨幣格式化的數字
currencySymbol: 貨幣符號,如¥,只適用於按照貨幣格式化的數字
groupingUsed: 是否包含分隔符
maxIntegerDigits: 整數部分最多顯示多少位
mixIntegerDigits: 整數部分最少顯示多少位
maxFractionDigits: 小數部分最多顯示多位位
minFractionDigits: 小數部分最少顯示多位位
var:存儲格式化後的結果
scope: 存儲的範圍
示例1:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="fr_fr"/>
- <fmt:formatNumber value="123456789.012"/>
- <br/>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="123456789.012"/>
- <br />
- <fmt:setLocale value="de_de"/>
- <fmt:formatNumber value="123456789.012"/>
- <br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="fr_fr"/>
- <fmt:formatNumber value="123456789.012"/>
- <br/>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="123456789.012"/>
- <br />
- <fmt:setLocale value="de_de"/>
- <fmt:formatNumber value="123456789.012"/>
- <br />
- </div>
- </div>
- </body>
- </html>
注意:如果要實現國際化,那麼編碼格式要設置爲utf-8.
從程序運行效果可以看出,設定的區域不同,格式化數字的顯示也會不同.
四:type屬性:可以是數字(number),貨幣(currency),百分比(percent)
示例2:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="number"/><br />
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:formatNumber value="0.3" type="percent"/><br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="number"/><br />
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:formatNumber value="0.3" type="percent"/><br />
- </div>
- </div>
- </body>
- </html>
currencyCode爲貨幣代碼,例如美元爲USD,人民幣爲CNY等 currencySymbol爲貨幣符號例如,人民幣爲¥,美元爲$。
如果不指定區域,則會根據語言區域自動選擇currencySymbol
示例3:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- </div>
- </div>
- </body>
- </html>
currencySymbol屬性還可以自定義要顯示的頭標識,但是一定得type="currency"纔會生效,例如:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency" currencySymbol="#"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency" currencySymbol="#"/><br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency" currencySymbol="#"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency" currencySymbol="#"/><br />
- </div>
- </div>
- </body>
- </html>
自定義數字樣式
<fmt:formatNumber value="12.31" pattern=".0000"/><br/>
<fmt:formatNumber value="1234" pattern="###.##E0"/>
會顯示:
12.3100
1.234E3
會四捨五入
var:定義一個變量,存儲格式化後的結果,scope指定變量存儲的範圍.用法和前面講的標籤一致.
五:<fmt:parseNumber>
此標籤用來將字符串類型的數字,貨幣或百分比轉換成數字類型,和<fmt:formatNumber>標籤的作用正好相反.
value: 要轉換的字符串
type: 指定要轉換的字符串爲什麼類型,可取值:number,percent,currency
pattern: 自定義格式化樣式
parseLocale: 指定區域來轉換字符串
IntegerOnly: 轉換後的數字是否只顯示整數部分
var: 存儲轉換後的結果
scope: 存儲的範圍
示例1:
<fmt:parseNumber value="500,800,200"/>
顯示: 500800200
示例2:
<fmt:parseNumber value="52%" type="percent" />
顯示: 0.52 (52%在這裏是一個字符串, type指定這個字符串是什麼類型的值)
示例3:
<fmt:parseNumber value="¥123" type="currency" />
顯示123, ¥123在這裏是一個字符串, type指定這個字符串是什麼類型的值
示例4:
<fmt:parseNumber value="123.333" type="number" /><br/>
<fmt:parseNumber value="123.333" type="number" integerOnly="true"/><br/>
顯示:
123.333
123
integerOnly確定是否只顯示整數部分.
示例5:
<fmt:parseNumber value="¥123.333" type="currency" parseLocale="zh_CN"/><br/>
<fmt:parseNumber value="$123.333" type="currency" parseLocale="en_US"/><br/>
parseLocale="en_US"主要是配合當type="currency"時用的,
如果要轉換貨幣的字符串類型爲value="¥123.333",不設置語言環境的話,會取當前瀏覽器的默認設置,否則就要加上parseLocale="zh_CN",指定環境爲中文環境
如果要轉換貨幣的字符串類型爲value="$123.333",不設置語言環境的話,會取當前瀏覽器的默認設置,如果默認爲zh_cn的話,程序會報錯的,否則就要加上parseLocale="en_US",指定環境爲英文美國環境
六:<fmt:formatDate />
此標籤可以將日期格式化.
屬性介紹:
value 用來格式化的時間或日期
type 指定格式化的是日期還是時間,或者兩者都是取值範圍:date,time,both
pattern 自定義格式化樣式
dateStyle 日期的格式化樣式
timeStyle 時間的格式化樣式
timeZone 指定使用的時區
var 存儲格式化後的結果
scope 指定存儲的範圍
示例1:
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" />
- <br />
- <fmt:setLocale value="zh_tw" />
- <fmt:formatDate value="<%=new Date()%>" />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" />
- <br />
- <fmt:setLocale value="zh_tw" />
- <fmt:formatDate value="<%=new Date()%>" />
大家可以看到大陸和臺灣顯示日期的格式是有區別的.
顯示結果:
2009-12-7
2009/12/7
示例2:
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="time"/>
<br />
<fmt:setLocale value="zh_tw" />
<fmt:formatDate value="<%=new Date()%>" type="time"/>
顯示結果:
14:59:28
下午 02:59:28
type可取值及意義:
date 格式化日期
time格式化時間
both格式化日期時間
示例3:
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" />
<br />
<fmt:setLocale value="zh_tw" />
<fmt:formatDate value="<%=new Date()%>" type="both" />
輸出結果:
2009-12-7 21:24:26
2009/12/7 下午 09:24:26
dateStyle用來設定日期顯示的樣式,其值可以是default, short, medium, long, full,請看示例:
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="default" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="short" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="medium" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="long" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="full" />
<br />
顯示結果如下:
2009-12-7 21:30:49
09-12-7 21:30:49
2009-12-7 21:30:49
2009年12月7日 21:30:49
2009年12月7日 星期一 21:30:49
可以看到dateStyle屬性只對日期部分起作用,時間部分沒有作用.
timeStyle用來顯示時間部分的樣式,取值範圍同上
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="default" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="short" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="medium" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="long" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="full" />
<br />
輸出:
2009-12-7 21:35:52
2009-12-7 下午9:35
2009-12-7 21:35:52
2009-12-7 下午09時35分52秒
2009-12-7 下午09時35分52秒 CST
timeZone用來設定時區,時區的意思類似於酒店裏大堂放的幾個時鐘,比如現在時間會有北京時間,東京時間,紐約時間,倫墩時間,
取值範圍爲:EST, CST, MST, PST
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="EST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="CST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="MST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="PST" />
<br />
輸出結果:
下午09時41分37秒 CST
上午08時41分37秒 EST
上午07時41分37秒 CST
上午06時41分37秒 MST
上午05時41分37秒 PST
自定義格式:
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
<br /> 注意這裏小時 hh表示12小時制, HH代表24小時制
七:<fmt:parseDate>
將字符串類型的時間轉換爲日期類型.
value 用來格式化的時間或日期的字符串
type 指定格式化的是日期還是時間,或者兩者都是取值範圍:date,time,both
pattern 自定義格式化樣式
dateStyle 日期的格式化樣式
timeStyle 時間的格式化樣式
timeZone 指定使用的時區
var 存儲格式化後的結果
scope 指定存儲的範圍
示例:
<fmt:setLocale value="zh_cn" />
<fmt:parseDate type="date" value="2008-4-5"/>
輸出: Sat Apr 05 00:00:00 CST 2008,
這裏已經將字符串” 2008-4-5”轉換爲了日期對象了.轉換一定得注意,類似於2008-4-5這樣的字符串,type必須爲date,類似於12:34:56的字符串,type必須爲time類似於2008-4-5 12:34:56這樣的字符串,type必須爲both還要注意瀏覽器的語言環境的設置,如果爲zh_tw,那麼字符串就必須得符合當地的標準,如爲2009/12/7 下午 09:24:26就正確轉換爲日期對象,否則就會報錯.
八:<fmt:setTimeZone>
value 設定時區
var 存儲設定的時區
scope 存儲的範圍
value用來設定時區,可以是EST,CST,MST,PST等,如果有var屬性,則將結果存儲在所設定的範圍之內.在屬性範圍內的頁面都會使用該時區爲默認時區.
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:setTimeZone value="EST" />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
輸出:
上午09時25分12秒 EST
上午09時25分12秒 EST
上午09時25分12秒 EST
此時區在該頁面內都有效
九:<fmt:timeZone>
用來暫時設置時區.
Java代碼
<fmt:setLocale value="zh_cn" />
<fmt:timeZone value="EST">
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
</fmt:timeZone>
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
此標籤的時區只是部分,在標籤開始至標籤結束內有效,其它地方無效,其它地方還是會使用默認時區