格式化标签

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" />

 

posted @ 2016-05-18 21:25  Sunny_star  阅读(2314)  评论(1编辑  收藏  举报