JSTL格式化標籤用來格式化並輸出文本、日期、時間、數字。html
引用方式:在jsp頁面加入<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>java
格式化標籤包括:git
標籤 | 描述 |
---|---|
<fmt:formatNumber> | 使用指定的格式或精度格式化數字 |
<fmt:parseNumber> | 解析一個表明着數字,貨幣或百分比的字符串 |
<fmt:formatDate> | 使用指定的風格或模式格式化日期和時間 |
<fmt:parseDate> | 解析一個表明着日期或時間的字符串 |
<fmt:bundle> | 綁定資源 |
<fmt:setLocale> | 指定地區 |
<fmt:setBundle> | 綁定資源 |
<fmt:timeZone> | 指定時區 |
<fmt:setTimeZone> | 指定時區 |
<fmt:message> | 顯示資源配置文件信息 |
<fmt:requestEncoding> | 設置request的字符編碼 |
<fmt:formatNumber> 使用指定的格式或精度格式化數字瀏覽器
語法:yii
<fmt:formatNumber value="" type="currency" [pattern=""] [currencyCode=""] [currencySymbol=""] [groupingUsed=""] [maxIntegerDigits=""] [minIntegerDigits=""] [maxFractionDigits=""] [minFractionDigits=""] [var=""] [scope=""]>jsp
<fmt:formatNumber value="" [type="currency|number|percent"] [pattern=""] [groupingUsed=""] [maxIntegerDigits=""] [minIntegerDigits=""] [maxFractionDigits=""] [minFractionDigits=""] [var=""] [scope=""]>ide
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 要顯示的數字 | 是 | 無 |
type | NUMBER,CURRENCY,或 PERCENT類型 | 否 | Number |
pattern | 指定一個自定義的格式化模式用與輸出 | 否 | 無 |
currencyCode | 貨幣碼(當type="currency"時) | 否 | 取決於默認區域 |
currencySymbol | 貨幣符號 (當 type="currency"時) | 否 | 取決於默認區域 |
groupingUsed | 是否對數字分組 (TRUE 或 FALSE) | 否 | true |
maxIntegerDigits | 整型數最大的位數 | 否 | 無 |
minIntegerDigits | 整型數最小的位數 | 否 | 無 |
maxFractionDigits | 小數點後最大的位數 | 否 | 無 |
minFractionDigits | 小數點後最小的位數 | 否 | 無 |
var | 存儲格式化數字的變量 | 否 | Print to page |
scope | var屬性的做用域 | 否 | page |
eg:ui
<%@ page language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>Tag Example - www.yiibai.com</title> </head> <body> <c:set var="balance" value="120000.2394" /> <c:out value="${balance}" /> <p>Formatted Number (1): <fmt:formatNumber value="${balance}" type="currency" /> </p> <p>Formatted Number (2): <fmt:formatNumber value="${balance}" type="number" maxIntegerDigits="3" /> </p> <p>Formatted Number (3): <fmt:formatNumber value="${balance}" type="number" maxFractionDigits="3" /> </p> <p>Formatted Number (4): <fmt:formatNumber value="${balance}" type="number" groupingUsed="false" /> </p> <p>Formatted Number (5): <fmt:formatNumber value="${balance}" type="percent" maxIntegerDigits="2" /> </p> <p>Formatted Number (6): <fmt:formatNumber value="${balance}" type="percent" maxFractionDigits="1" /> </p> <p>Formatted Number (7): <fmt:formatNumber value="${balance}" type="percent" minFractionDigits="3" /> </p> <p>Formatted Number (8): <fmt:formatNumber value="${balance}" type="number" pattern="##.###E00" /> </p> <p>Currency in USA: <fmt:setLocale value="en_US" /> <fmt:formatNumber value="${balance}" type="currency" /> </p> </body> </html> //結果輸出爲: 120000.2394 Formatted Number (1): ¥120,000.24 Formatted Number (2): 000.239 Formatted Number (3): 120,000.239 Formatted Number (4): 120000.239 Formatted Number (5): 24% Formatted Number (6): 12,000,023.9% Formatted Number (7): 12,000,023.940% Formatted Number (8): 12E04 Currency in USA: $120,000.24
<fmt:parseNumber> 解析一個表明着數字,貨幣或百分比的字符串編碼
語法:spa
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 要解析的數字 | 否 | Body |
type | NUMBER,,CURRENCY,或 PERCENT | 否 | number |
parseLocale | 解析數字時所用的區域 | 否 | 默認區域 |
integerOnly | 是否只解析整型數(true)或浮點數(false) | 否 | false |
pattern | 自定義解析模式 | 否 | 無 |
timeZone | 要顯示的日期的時區 | 否 | 默認時區 |
var | 存儲待解析數字的變量 | 否 | Print to page |
scope | var屬性的做用域 | 否 | page |
eg:
<%@ page language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>Tag Example - www.yiibai.com</title> </head> <body> <c:set var="balance" value="125003.350" /> <fmt:parseNumber value="${balance}" var="i" type="number" /> <p>Parsed Number(1):<c:out value="${i}" /></p> <fmt:parseNumber value="${balance}" var="i" type="number" integerOnly="true" /> <p>Parsed Number(2):<c:out value="${i}" /> </body> </html> //結果輸出爲: Parsed Number(1):125003.35 Parsed Number(2):125003
<fmt:formatDate> 使用指定的風格或模式格式化日期和時間
語法:
<fmt:formatDate value="" [type="date|time|both"]>
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 要顯示的日期 | 是 | 無 |
type | DATE, TIME, 或 BOTH | 否 | date |
dateStyle | FULL, LONG, MEDIUM, SHORT, 或 DEFAULT | 否 | default |
timeStyle | FULL, LONG, MEDIUM, SHORT, 或 DEFAULT | 否 | default |
pattern | 自定義格式模式 | 否 | 無 |
timeZone | 顯示日期的時區 | 否 | 默認時區 |
var | 存儲格式化日期的變量名 | 否 | 顯示在頁面 |
scope | 存儲格式化日誌變量的範圍 | 否 | 頁面 |
格式:
代碼 | 描述 | 實例 |
---|---|---|
G |
時代標誌 |
AD |
y |
不包含紀元的年份。若是不包含紀元的年份小於 10,則顯示不具備前導零的年份。 |
2002 |
M |
月份數字。一位數的月份沒有前導零。 |
April & 04 |
d |
月中的某一天。一位數的日期沒有前導零。 |
20 |
h |
12 小時制的小時。一位數的小時數沒有前導零。 |
12 |
H |
24 小時制的小時。一位數的小時數沒有前導零。 |
0 |
m |
分鐘。一位數的分鐘數沒有前導零。 |
45 |
s |
秒。一位數的秒數沒有前導零。 |
52 |
S |
毫秒 |
970 |
E |
周幾 |
Tuesday |
D |
一年中的第幾天 |
180 |
F |
一個月中的第幾個周幾 |
2 (一個月中的第二個星期三) |
w |
一年中的第幾周r |
27 |
W |
一個月中的第幾周 |
2 |
a |
a.m./p.m. 指示符 |
PM |
k |
小時(12 小時制的小時) |
24 |
K |
小時(24 小時制的小時) |
0 |
z |
時區 |
中部標準時間 |
' |
轉義文本 |
|
'' |
單引號 |
eg:
<%@ page language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>Tag Example - www.yiibai.com</title> </head> <body> <c:set var="now" value="<%=new java.util.Date() %>" /> <p>Formatted Date (1): <fmt:formatDate value="${now}" type="time" /> </p> <p>Formatted Date (2): <fmt:formatDate value="${now}" type="date" /> </p> <p>Formatted Date (3): <fmt:formatDate value="${now}" type="both" /> </p> <p>Formatted Date (4): <fmt:formatDate value="${now}" type="both" dateStyle="short" timeStyle="short" /> </p> <p>Formatted Date (5): <fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="medium" /> </p> <p>Formatted Date (6): <fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="short" /> </p> <p>Formatted Date (7): <fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long" /> </p> <p>pattern: <fmt:formatDate value="${now}" pattern="yyyy-MM-dd" /> </p> </body> </html> //結果輸出爲: Formatted Date (1): 11:55:11 Formatted Date (2): 2016-5-14 Formatted Date (3): 2016-5-14 11:55:11 Formatted Date (4): 16-5-14 上午11:55 Formatted Date (5): 2016-5-14 11:55:11 Formatted Date (6): 2016-5-14 上午11:55 Formatted Date (7): 2016年5月14日 上午11時55分11秒 pattern: 2016-05-14
<fmt:parseDate> 解析一個表明着日期或時間的字符串
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 要顯示的日期 | 是 | 無 |
type | DATE, TIME, 或 BOTH | 否 | date |
dateStyle | FULL, LONG, MEDIUM, SHORT, 或 DEFAULT | 否 | default |
timeStyle | FULL, LONG, MEDIUM, SHORT, 或 DEFAULT | 否 | default |
pattern | 自定義格式模式 | 否 | 無 |
timeZone | 顯示日期的時區 | 否 | 默認時區 |
var | 存儲格式化日期的變量名 | 否 | 顯示在頁面 |
scope | 存儲格式化日誌變量的範圍 | 否 | 頁面 |
eg:
<%@ page language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>Tag Example - www.yiibai.com</title> </head> <body> <c:set var="now" value="20-10-2010" /> <fmt:parseDate value="${now }" pattern="dd-MM-yyyy" type="date" dateStyle="short"/> <%-- <p>Parse Date:<c:out value="${parsedEmpDate}" /></p> --%> </body> </html> //結果輸出爲: Wed Oct 20 00:00:00 CST 2010
<fmt:bundle> 綁定資源
資源束包含區域特定對象。資源束包含鍵值對。
當程序須要區域特定資源時,能夠將全部的關鍵詞對全部的locale共享,也能夠爲locale指定轉換後的值。資源束能夠幫助提供指定的locale的內容。
一個Java資源束文件包含一系列的鍵值對。所需類繼承自java.util.ListResourceBundle類。類編譯後放在Web應用程序的CLASSPATH中。
語法:
<fmt:bundle basename="basename" [prefix="keypre"]>
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
basename | 指定被載入的資源束的基礎名稱 | 是 | 無 |
prefix | 指定<fmt:message>標籤key屬性的前綴 | 否 | 無 |
eg:
1.java資源束建立部分
package com.tutorialspoint; import java.util.ListResourceBundle; public class Example_En extends ListResourceBundle{ static final Object[][] contents={ {"count.one","One"}, {"count.two","Two"}, {"count.three","Three"} }; @Override protected Object[][] getContents() { // TODO Auto-generated method stub return contents; } }
2.資源束使用部分
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <fmt:bundle basename="com.tutorialspoint.Example_En" prefix="count."> <fmt:message key="one" /><br> <fmt:message key="two" /><br> <fmt:message key="three" /><br> </fmt:bundle> <fmt:bundle basename="com.tutorialspoint.Example_En"> <fmt:message key="count.one" /><br> <fmt:message key="count.two" /><br> <fmt:message key="count.three" /><br> </fmt:bundle> </body> </html>
//結果輸出爲:
One
Two Three
One
Two Three
<fmt:setLocale> 指定地區(將給定的區域存儲在locale配置變量中)
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 指定ISO-639 語言碼和ISO-3166 國家碼 | 是 | en_US |
variant | 特定瀏覽器變體 | 否 | 無 |
scope | Locale配置變量的做用域 | 否 | Page |
eg:
1.java資源束建立部分Example_en_US
package com.tutorialspoint; import java.util.ListResourceBundle; public class Example_en_US extends ListResourceBundle{ static final Object[][] contents={ {"count.one","One"}, {"count.two","Two"}, {"count.three","Three"} }; @Override protected Object[][] getContents() { // TODO Auto-generated method stub return contents; } }
2.java資源束建立部分Example_es_ES
package com.tutorialspoint; import java.util.ListResourceBundle; public class Example_es_ES extends ListResourceBundle{ static final Object[][] contents={ {"count.one","Uno"}, {"count.two","Dos"}, {"count.three","Tres"} }; @Override protected Object[][] getContents() { // TODO Auto-generated method stub return contents; } }
3.資源束使用部分
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <fmt:bundle basename="com.tutorialspoint.Example" prefix="count."> <fmt:message key="one" /><br> <fmt:message key="two" /><br> <fmt:message key="three" /><br> </fmt:bundle> <fmt:setLocale value="es_ES" /> <fmt:bundle basename="com.tutorialspoint.Example"> <fmt:message key="count.one" /><br> <fmt:message key="count.two" /><br> <fmt:message key="count.three" /><br> </fmt:bundle> </body> </html>
//結果輸出爲:
One
Two Three
Uno
Dos Tres
<fmt:setBundle> 綁定資源(載入一個資源束,而後存儲在做用域中已命名的變量或bundle配置變量中)類似於<fmt:bundle>
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
basename | 資源束家族基礎名稱,暴露給做用域變量或配置變量 | 是 | 無 |
var | 存儲新資源束的變量 | 否 | Replace default |
scope | 變量的做用域 | 否 | Page |
eg
1.java資源束建立部分Example_es_ES
package com.tutorialspoint; import java.util.ListResourceBundle; public class Example_es_ES extends ListResourceBundle{ static final Object[][] contents={ {"count.one","Uno"}, {"count.two","Dos"}, {"count.three","Tres"} }; @Override protected Object[][] getContents() { // TODO Auto-generated method stub return contents; } }
2.資源束使用部分
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <fmt:setLocale value="es_ES" /> <fmt:setBundle basename="com.tutorialspoint.Example" var="lang" /> <fmt:message key="count.one" bundle="${lang}"/><br> <fmt:message key="count.two" bundle="${lang}"/><br> <fmt:message key="count.three" bundle="${lang}"/><br> </body> </html>
//結果輸出爲:
Uno
Dos Tres
<fmt:timeZone> 指定時區
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 時區 | 是 | 無 |
eg:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <c:set var="now" value="<%=new java.util.Date() %>" /> <p>當前時間: <fmt:formatDate value="${now}" type="both" timeStyle="medium" dateStyle="medium" /> </p> <p>時區爲 GMT-8 : <fmt:timeZone value="GMT-8"> <fmt:formatDate value="${now}" type="both" /> </fmt:timeZone> </p> </body> </html>
//結果輸出爲:
當前時間: 2016-5-18 20:54:04
時區爲 GMT-8 : 2016-5-18 4:54:04
<fmt:setTimeZone> 用來複制一個時區對象至指定的做用域,相似於<fmt:timeZone>
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 時區 | 是 | 無 |
var | 存儲新時區的變量名 | 否 | Replace default |
scope | 變量的做用域 | 否 | Page |
eg:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <c:set var="now" value="<%=new java.util.Date() %>" /> <p>當前時間: <fmt:formatDate value="${now}" type="both" timeStyle="medium" dateStyle="medium" /> </p> <p>時區爲 GMT-8 : <fmt:setTimeZone value="GMT-8" /> <fmt:formatDate value="${now}" type="both" /> </p> </body> </html>
//結果輸出爲:
當前時間: 2016-5-18 20:54:04
時區爲 GMT-8 : 2016-5-18 4:54:04
<fmt:message> 映射一個關鍵字給局部消息,而後執行參數替換
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
key | 要檢索的消息關鍵字 | 否 | Body |
bundle | 要使用的資源束 | 否 | 默認資源束 |
var | 存儲局部消息的變量名 | 否 | Print to page |
scope | var屬性的做用域 | 否 | Page |
eg:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <fmt:setLocale value="es_ES" /> <fmt:setBundle basename="com.tutorialspoint.Example" var="lang" /> <fmt:message key="count.one" bundle="${lang}"/><br> <fmt:message key="count.two" bundle="${lang}"/><br> <fmt:message key="count.three" bundle="${lang}"/><br> </body> </html> //結果輸出爲: Uno Dos Tres
<fmt:requestEncoding> 用來指定返回給Web應用程序的表單編碼類型
指定字符集,用於解碼來自表單的數據。在字符集不是ISO-8859-1時必須使用這個標籤。
用來指定請求的Content-Type,即便response是經過Page指令的contentType屬性來編碼,這是因爲response的實際區域可能與Page指令所指定的不一樣。
語法:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
value | 字符編碼集的名稱,用於解碼request參數 | 是 | 無 |
eg:
<fmt:requestEncoding value="UTF-8" />