格式化標簽


JSTL格式化標簽用來格式化並輸出文本、日期、時間、數字。

引用方式:在jsp頁面加入<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

格式化標簽包括:

標簽 描述
<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> 使用指定的格式或精度格式化數字

語法:

<fmt:formatNumber value="" type="currency" [pattern=""] [currencyCode=""] [currencySymbol=""] [groupingUsed=""] [maxIntegerDigits=""] [minIntegerDigits=""] [maxFractionDigits=""] [minFractionDigits=""] [var=""] [scope=""]>

<fmt:formatNumber value="" [type="currency|number|percent"] [pattern=""] [groupingUsed=""] [maxIntegerDigits=""] [minIntegerDigits=""] [maxFractionDigits=""] [minFractionDigits=""] [var=""] [scope=""]>

 

屬性 描述 是否必要 默認值
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:

<%@ 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> 解析一個代表着數字,貨幣或百分比的字符串

語法:

屬性 描述 是否必要 默認值
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" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM