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. MVC 中使用log4net 打印重复日志解决方法

    最近在项目中引用log4net 来打印日志,会发现在同一时间点 打印重复记录: 详见图

  2. [转]Reed Solomon纠删码

    [转]Reed Solomon纠删码    http://peterylh.blog.163.com/blog/static/12033201371375050233/     纠删码是存储领域常用的 ...

  3. [转]Linux Ubuntu上架设FTP

    Linux Ubuntu上架设FTP http://www.blogjava.net/stonestyle/articles/369104.html 操作系统:ubuntu (GNU/Linux) 为 ...

  4. 关于使用,NI采集卡+labview信号采集,问题交流【第二贴】

    *** 采集卡 NI PCI-6534:  max sample rate 20MHz:32位的数字输入,数字输出.  ***输入信号:    峰峰值,4.16V   最小值 -80mV    频率 ...

  5. Java Day 08

    数组工具对象建立 多个.java文件先编译谁 数组工具类中静态的使用 构造函数私有化 数组工具类--文档注释 设计模式 -- 单例设计模式 保证一个类在内存中的对象唯一性 1.不允许其他程序使用new ...

  6. Lisp使用Lambda语法

    lamdba 其实就是一个匿名函数. 定义Lisp的lambda语法非常的简单,如下: (lambda ([parameter]) [experssion]) 调用lambda的语法有三种方法,如下: ...

  7. javascript面向对象分层思维

    js本身不是面向对象语言,在我们实际开发中其实很少用到面向对象思想,以前一直以为当要复用的时候才封装成对象,然而随着现在做的项目都后期测试阶段发现面向对象的作用不仅仅只是复用,可能你们会说面向对象还有 ...

  8. c++ goto的使用

    c++ goto 语句的使用 1.定义一个类似标签的东西lable 2.使用goto关键字,跳转到lable, goto lable #include <iostream> #includ ...

  9. C/C++常用头文件及函数汇总

    转自: C/C++常用头文件及函数汇总 C/C++头文件一览 C #include <assert.h> //设定插入点#include <ctype.h> //字符处理#in ...

  10. LNMP系列网站零基础开发记录(一)

    [目录] 扯淡吹逼之开发前奏 Django 开发环境搭建及配置 web 页面开发 Django app开发 Django 站点管理 Python 简易爬虫开发 Nginx&uWSGI 服务器配 ...