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. scjp考试准备 - 2 - 逻辑运算及类型转换

    判断如下代码的执行结果: public class Spock{ public static void main(String[] args){ Long tail = 2000L; Long dis ...

  2. How to write a windows service

    how to write a windows services susport microsoft This aritcle describe the detail step to setup a w ...

  3. R语言 如何为图片添加文字说明(转载)

    转载:(中文翻译者)[http://blog.csdn.net/chen790646223/article/details/49766659] (原文链接)[http://datascienceplu ...

  4. oracle11g创建数据库最后一步确定时弹出无法创建目录

    总的说是Windows7的权限问题 Windows7 dos命令下输入dbca创建数据库,因为权限问题,数据库将无法完成.所以还是在开始程序中打开dbca创建数据库比较好. Windows7 dos以 ...

  5. ubuntu 屏幕截图

    无意间发现这个组合 Fn+Shift+t,按下之后,鼠标变成一个十字, 选择一个范围,okay,可以截图了

  6. < java.util >-- Collection接口

    Collection:    |--List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引.元素可以重复.    |--Set:无序(存入和取出顺序有可能不一致),不可以存储重复元素.必须 ...

  7. 利用while(code!=EOF){}来实现“无限”循环

    #include <stdio.h>int main(){    char a,b,c;    char t;    while(scanf("%c%c%c",& ...

  8. Node.js 学习(六)Node.js EventEmitter

    Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一个fs.read ...

  9. 【Convert Sorted Array to Binary Search Tree】cpp

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  10. Introduction to Financial Management

    Recently,i am learning some useful things about financial management by reading <Essentials of Co ...