格式化标签

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要显示的数字
typeNUMBER,CURRENCY,或 PERCENT类型Number
pattern指定一个自定义的格式化模式用与输出
currencyCode货币码(当type="currency"时)取决于默认区域
currencySymbol货币符号 (当 type="currency"时)取决于默认区域
groupingUsed是否对数字分组 (TRUE 或 FALSE)true
maxIntegerDigits整型数最大的位数
minIntegerDigits整型数最小的位数
maxFractionDigits小数点后最大的位数
minFractionDigits小数点后最小的位数
var存储格式化数字的变量Print to page
scopevar属性的作用域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
typeNUMBER,,CURRENCY,或 PERCENTnumber
parseLocale解析数字时所用的区域默认区域
integerOnly是否只解析整型数(true)或浮点数(false)false
pattern自定义解析模式
timeZone要显示的日期的时区默认时区
var存储待解析数字的变量Print to page
scopevar属性的作用域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要显示的日期
typeDATE, TIME, 或 BOTHdate
dateStyleFULL, LONG, MEDIUM, SHORT, 或 DEFAULTdefault
timeStyleFULL, LONG, MEDIUM, SHORT, 或 DEFAULTdefault
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要显示的日期
typeDATE, TIME, 或 BOTHdate
dateStyleFULL, LONG, MEDIUM, SHORT, 或 DEFAULTdefault
timeStyleFULL, LONG, MEDIUM, SHORT, 或 DEFAULTdefault
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特定浏览器变体
scopeLocale配置变量的作用域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
scopevar属性的作用域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" />

 

转载于:https://www.cnblogs.com/blog-yuesheng521/p/5468624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值