JSTL的formatting tags可以用来格式化和显示文本、日期、时间、数字。如果在JSP页面中要用到该库提供的tag的话,需要引入如下taglib:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

1. <fmt: formatNumber>

该tag包含有以下一些属性:

Attribute Description Required Default
value 需要显示的数字 Yes None
type NUMBER, CURRENCY, or PERCENT No Number
pattern 自己定义一种输出格式 No None
currencyCode Currency code (for type="currency") No From the default locale
currencySymbol 货币符号 No From the default locale
groupingUsed 是否分组显示(TRUE or FALSE) No true
maxIntegerDigits 最多显示的整数位数 No None
minIntegerDigits 至少显示的整数位数 No None
maxFractionDigits 最多显示的小数位数 No None
minFractionDigits 最少显示的小数位数  No None
var 存储格式化后的值的变量 No Print to page
scope 变量的Scope No page

如果想要自己定义一种格式显示的话,它也给我们提供了一些转义的属性:

Symbol Description
0 表示一个数字
E 表示使用科学计数法
# 表示一个数字,默认显示0
. 整数和小数的分隔符
, 分组的分隔符
; 分割不同的格式
- 默认的负数符号
% 乘以100后以百分比显示
? 乘以1000后以千分比显示
¤ 货币符号的占位符
X 表示任何其他的字符都可以用于前缀和后缀
' 在前缀后后缀中引用特殊符号

Example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <c:setvar="balance"value="120000.2309"/>
<p>Formatted Number (1): <fmt:formatNumbervalue="${balance}"type="currency"/></p>
<p>Formatted Number (2): <fmt:formatNumbertype="number"maxIntegerDigits="3"value="${balance}"/></p>
<p>Formatted Number (3): <fmt:formatNumbertype="number"maxFractionDigits="3"value="${balance}"/></p>
<p>Formatted Number (4): <fmt:formatNumbertype="number"groupingUsed="false"value="${balance}"/></p>
<p>Formatted Number (5): <fmt:formatNumbertype="percent"maxIntegerDigits="3"value="${balance}"/></p>
<p>Formatted Number (6): <fmt:formatNumbertype="percent"minFractionDigits="10"value="${balance}"/></p>
<p>Formatted Number (7): <fmt:formatNumbertype="percent"maxIntegerDigits="3"value="${balance}"/></p>
<p>Formatted Number (8): <fmt:formatNumbertype="number"pattern="###.###E0"value="${balance}"/></p>
<p>Currency in USA :
<fmt:setLocalevalue="en_US"/>
<fmt:formatNumbervalue="${balance}"type="currency"/></p>

NUMBER FORMAT:

Formatted Number (1): £120,000.23
Formatted Number (2): 000.231
Formatted Number (3): 120,000.231
Formatted Number (4): 120000.231
Formatted Number (5): 023%
Formatted Number (6): 12,000,023.0900000000%
Formatted Number (7): 023%
Formatted Number (8): 120E3
Currency in USA : $120,000.23

2. <fmt: parseNumber>
     它用来解析数字、百分比、货币。包含以下属性:

Attribute Description Required Default
value 需要被解析的值 No Body
type NUMBER, CURRENCY, or PERCENT No number
parseLocale 解析时需要的区域设置 No Default locale
integerOnly 是否只解析整数 (true)或者浮点数(false) No false
pattern 自定义的格式 No None
timeZone 显示日期的时区 No Default time zone
var 存储解析完之后的值 No Print to page
scope var生存的Scope No page

Example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <c:setvar="balance"value="1250003.350"/> <fmt:parseNumbervar="i"type="number"value="${balance}"/>
<p>Parsed Number (1) : <c:outvalue="${i}"/></p>
<fmt:parseNumbervar="i"integerOnly="true"type="number"value="${balance}"/>
<p>Parsed Number (2) : <c:outvalue="${i}"/></p>
NUMBER PARSING:
Parsed Number (1) : 1250003.35
Parsed Number (2) : 1250003

3. <fmt: formatDate>
     用来格式化日期和时间,它包含以下属性:

Attribute Description Required Default
value 需要格式化的值 Yes None
type DATE, TIME, or BOTH No date
dateStyle FULL, LONG, MEDIUM, SHORT, or DEFAULT No default
timeStyle FULL, LONG, MEDIUM, SHORT, or DEFAULT No default
pattern 自定义格式 No None
timeZone 显示时间的时区 No Default time zone
var 存储格式化后的值的变量 No Print to page
scope 变量的Scope No page

下面是一些可以用在自定义格式时的转义字符:

Code Purpose Sample
G 纪元标记 AD
y 年 2002
M 月 April & 04
d 日 20
h 时(12小时制) 12
H 时(24小时制)  0
m 分 45
s 秒 52
S 毫秒 970
E 星期 Tuesday
D 一年中的第几天 180
F 一个月中的第几个星期几 2 (2nd Wed in month)
w 一年中的第几个星期 27
W 一个月中的第几个星期  2
a a.m./p.m. 标记 PM
k The hour(12-hour time) 24
K The hour(24-hour time) 0
z The time zone Central Standard Time
'   The escape for text
''   The single quote

Example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <c:set var="now" value="<%=new java.util.Date()%>" /> <p>Formatted Date (1): <fmt:formatDatetype="time"value="${now}"/></p>
<p>Formatted Date (2): <fmt:formatDatetype="date"value="${now}"/></p>
<p>Formatted Date (3): <fmt:formatDatetype="both"value="${now}"/></p>
<p>Formatted Date (4): <fmt:formatDatetype="both"dateStyle="short"timeStyle="short"value="${now}"/></p>
<p>Formatted Date (5): <fmt:formatDatetype="both"dateStyle="medium"timeStyle="medium"value="${now}"/></p>
<p>Formatted Date (6): <fmt:formatDatetype="both"dateStyle="long"timeStyle="long"value="${now}"/></p>
<p>Formatted Date (7): <fmt:formatDatepattern="yyyy-MM-dd"value="${now}"/></p>
DATE FORMAT:
Formatted Date (1): 14:27:18
Formatted Date (2): 23-Sep-2010
Formatted Date (3): 23-Sep-2010 14:27:18
Formatted Date (4): 23/09/10 14:27
Formatted Date (5): 23-Sep-2010 14:27:18
Formatted Date (6): 23 September 2010 14:27:18 GST
Formatted Date (7): 2010-09-23

4. <fmt: parseDate>
     用来解析日期,它包含以下属性:

Attribute Description Required Default
value 需要解析的日期 No Body
type DATE, TIME, or BOTH No date
dateStyle FULL, LONG, MEDIUM, SHORT, or DEFAULT No Default
timeStyle FULL, LONG, MEDIUM, SHORT, or DEFAULT No Default
parseLocale 解析日期时用的区域设置 No Default locale
pattern 自定义格式 No None
timeZone Time zone of the parsed date No Default time zone
var 存储解析后的值的变量 No Print to page
scope 变量的Scope No page

Example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <c:setvar="now"value="20-10-2010"/> <fmt:parseDatevalue="${now}"var="parsedEmpDate"pattern="dd-MM-yyyy"/>
<p>Parsed Date: <c:outvalue="${parsedEmpDate}"/></p>
DATE PARSING:
Parsed Date: Wed Oct 20 00:00:00 GST 2010

还有一些其他的tag,以前没用用过,也没看见别人用过。还是列在下面:

Tag Description
…… ……
<fmt:bundle> Loads a resource bundle to be used by its tag body.
<fmt:setLocale> Stores the given locale in the locale configuration variable.
<fmt:setBundle> Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
<fmt:timeZone> Specifies the time zone for any time formatting or parsing actions nested in its body.
<fmt:setTimeZone> Stores the given time zone in the time zone configuration variable
<fmt:message> To display an internationalized message.
<fmt:requestEncoding> Sets the request character encoding.

  

  文中的例子例子:http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

JSTL Tag学习笔记(二)之<fmt: />的更多相关文章

  1. JSTL Tag学习笔记(一)之<c: />

    注:本文中的例子主要来自http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm.  ======================= ...

  2. JSTL Tag学习笔记之<fn: />

    在JSTL中也提供了一些标准的函数,但是几乎都是和操作字符串相关的函数,如果需要使用这类函数的话,应该引入下面的taglib: <%@ taglib prefix="fn" ...

  3. Go语言学习笔记二: 变量

    Go语言学习笔记二: 变量 今天又学了一招如何查看go的版本的命令:go version.另外上一个笔记中的代码还可以使用go run hello.go来运行,只是这种方式不会生成exe文件. 定义变 ...

  4. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  5. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  6. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

  7. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  8. java之jvm学习笔记二(类装载器的体系结构)

    java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...

  9. Java IO学习笔记二

    Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...

随机推荐

  1. [转]强悍的跨平台开源多媒体中心XBMC介绍

    [转]强悍的跨平台开源多媒体中心XBMC介绍 http://www.cnblogs.com/mythou/p/3220898.html 最近都在了解Android下的音视频,因为最近需要做一个多媒体中 ...

  2. < java.lang >-- StringBuffer字符串缓冲区

    构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符. 特点: 1:可以对字符串内容进行修改. 2:是一个容器. 3:是可变长度的. 4:缓冲区中可以存储任意类型的数据. 5:最终需要变成字符 ...

  3. 关于VS2010error RC2170 : bitmap file res\tmp1.bmp is not in 3.00 format

      我们有时候向VS中的程序插入图片,会出现如下错误: 这是VS的一个bug,对于不能识别的资源,添加的时候,VS会弹出一个对话框让你填类型,这个类型其实是字符串表示,而不是像内置类型,例如整数. 解 ...

  4. 微软职位内部推荐-Senior Software Engineer

    微软近期Open的职位: Are you looking for a big challenge? Do you know why Big Data is the next frontier for ...

  5. 论单页Web应用和RESTful架构

    单页Web应用 概述 单页Web应用并不是突然诞生的一门新技术,而是web展示的一种新的尝试.它将所有的动作局限于一个Web页面,在加载站点首页的时候就加载站点需要的JavaScript和CSS.单页 ...

  6. PBOC2.0与PBOC3.0的区别

    2013年2月,中国人民银行发布了<中国金融集成电路(IC)卡规范(V3.0)>(以下简称PBOC3.0),PBOC3.0是在中国人民银行2005年颁布的<中国金融集成电路(IC)卡 ...

  7. C中的一些函数

    简述:printf.sprintf函数 转载自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html 部分进行了修改,参考http:/ ...

  8. JS 面向对象之继承 -- 原型链

    ECMAScript只支持实现继承,其实现继承主要是靠原型链来实现. 原型链的基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法. 简单回顾下构造函数.原型和实例的关系: 每个构造函数都有 ...

  9. 读书笔记:<世界是数字的>

    世界是数字的?第一次听到这个名词不由的感到惊讶,我有听过地球是海洋的,那世界不应该是人类共同拥有的吗或者也可以说世界是大自然的?为什么世界偏偏是数字的呢?我带着满心的疑问去看了那本<世界是数字的 ...

  10. [转载]Extjs中的dom,Ext.Element和Ext.Component对象的关系

    原文地址:http://www.cnblogs.com/lwzz/archive/2011/01/30/1948106.html   Ext.Element对象是对dom对象的封装,目的是为了跨平台以 ...