JSTL Tag学习笔记(二)之<fmt: />
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
作者:make dream
出处:http://www.cnblogs.com/penghongwei/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号