16.2.0.Jstl国际化标签库<fmt:

发表日期:2015-02-15 21:20:08 | 来源: | | 浏览(751) 分类:JSP基础

Jstl国际化标签库<fmt:其实就是文本、日期的格式化操作,format。

<fmt:bundle basename=""></fmt:bundle>
<fmt:formatDate value=""/>
<fmt:formatNumber></fmt:formatNumber>
<fmt:message></fmt:message>
<fmt:param></fmt:param>
<fmt:parseDate></fmt:parseDate>
<fmt:parseNumber></fmt:parseNumber>
<fmt:requestEncoding/>
<fmt:setBundle basename=""/>
<fmt:setLocale value=""/>
<fmt:setTimeZone value=""/>
<fmt:timeZone value=""></fmt:timeZone>


实例:

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSTL-c:核心标签库if</title>
</head>
<body>
<%
pageContext.setAttribute("date", new Date());
%>


<h4>系统显示:${date}</h4>//Thu Feb 12 00:19:35 CST 2015

<hr/>

<h4>
<fmt:formatDate value="${date}" type="both" dateStyle="default" timeStyle="default" var="dt" />
默认显示:${dt}
</h4>//2015-2-12 0:19:35

<h4>
<fmt:formatDate value="${date}" type="both" dateStyle="short" timeStyle="short" var="dt" />
简短显示:${dt}
</h4>//15-2-12 上午12:19

<h4>
<fmt:formatDate value="${date}" type="both" dateStyle="medium" timeStyle="medium" var="dt" />
中等显示:${dt}
</h4>//2015-2-12 0:19:35

<h4>
<fmt:formatDate value="${date}" type="both" dateStyle="long" timeStyle="long" var="dt" />
较长显示:${dt}
</h4>//2015年2月12日 上午12时19分35秒

<h4>
<fmt:formatDate value="${date}" type="both" dateStyle="full" timeStyle="full" var="dt" />
完整显示:${dt}
</h4>//2015年2月12日 星期四 上午12时19分35秒 CST

<h4>
<fmt:formatDate value="${date}" type="both" pattern="yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" var="dt" />
指定格式:${dt}
</h4>//2015年02月12日 00时19分35秒880毫秒

<hr/>

<fmt:parseDate value="2015年2月12日 星期四 上午12时08分26秒 CST" type="both" dateStyle="full" timeStyle="full" var="pdt"/>
<h4>反格式化日期:${pdt}</h4>//Thu Feb 12 00:08:26 CST 2015

<fmt:parseDate value="2015年02月12日 00时18分44秒860毫秒" type="both" pattern="yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" var="pdt"/>
<h4>反格式化指定格式日期:${pdt}</h4>//Thu Feb 12 00:18:44 CST 2015

<hr/>
<h4>
<fmt:setTimeZone value="HST"/>
<fmt:formatDate value="${date}" type="both" dateStyle="full" timeStyle="full" var="dt" />
设置时区显示:${dt}
</h4>//2015年2月11日 星期三 上午06时27分59秒 HST

<hr/>

<h4>
<fmt:setLocale value="en_us"/>
设置英文显示:<fmt:formatDate value="${date}"/>
</h4>//Feb 12, 2015

<h4>
<fmt:setLocale value="ZH_CN"/>
设置中文显示:<fmt:formatDate value="${date}"/>
</h4>//2015-2-12

<hr/>

<fmt:requestEncoding value="UTF-8"/>

<!--
fmt:bundle
在WEB-INF/classes/文件夹下建立一个msg.properties的文件写入:
name = ENIAC
age = he is {0} years old!
 -->
<fmt:bundle basename="msg">
<fmt:message key="name" var="nameRef"/>
</fmt:bundle>
<h4>姓名:${nameRef}</h4>//ENIAC

<fmt:bundle basename="msg">
<fmt:message key="age" var="ageRef">
<fmt:param value="34"/>
</fmt:message>
</fmt:bundle>
<h4>年龄:${ageRef}</h4>//he is 34 years old!

<hr/>

<fmt:setBundle basename="msg" var="m"/>
<fmt:message key="name" var="nameRef" bundle="${m}"/>
<fmt:message key="age" var="ageRef" bundle="${m}">
<fmt:param value="34"/>
</fmt:message>
<h4>姓名:${nameRef}</h4>//ENIAC
<h4>年龄:${ageRef}</h4>//he is 34 years old!
<!-- 其实fmt:setBundle就是bundle的另一种写法而已 -->

<hr/>

<fmt:formatNumber value="1458744645.65448841" maxIntegerDigits="7" maxFractionDigits="3" groupingUsed="true" var="num"/>
<h4>${num }</h4>//8,744,645.654

<fmt:formatNumber value="1458744645.65448841" maxFractionDigits="3" groupingUsed="true" var="num"/>
<h4>${num }</h4>//1,458,744,645.654

<fmt:formatNumber value="1458744645.65448841" maxFractionDigits="1" groupingUsed="true" var="num"/>
<h4>${num }</h4>//1,458,744,645.7

<fmt:formatNumber value="1458744645.65448841" maxFractionDigits="1" groupingUsed="false" var="num"/>
<h4>${num }</h4>//1458744645.7

<fmt:formatNumber value="1458744645.65448841" pattern="##.###E0" var="num"/>
<h4>${num }</h4>//14.587E8

<hr/>

<fmt:parseNumber value="14.587E8" pattern="##.###E0" var="num"/>
<h4>反格式化科学计数法:${num}</h4>//1458700000

<fmt:parseNumber value="3.2%" pattern="00%" var="num"/>
<h4>反百分比:${num}</h4>//0.032

</body>
</html>


显示:

系统显示:Thu Feb 12 00:30:56 CST 2015

//Thu Feb 12 00:19:35 CST 2015


默认显示:2015-2-12 0:30:56

//2015-2-12 0:19:35

简短显示:15-2-12 上午12:30

//15-2-12 上午12:19

中等显示:2015-2-12 0:30:56

//2015-2-12 0:19:35

较长显示:2015年2月12日 上午12时30分56秒

//2015年2月12日 上午12时19分35秒

完整显示:2015年2月12日 星期四 上午12时30分56秒 CST

//2015年2月12日 星期四 上午12时19分35秒 CST

指定格式:2015年02月12日 00时30分56秒632毫秒

//2015年02月12日 00时19分35秒880毫秒


反格式化日期:Thu Feb 12 00:08:26 CST 2015

//Thu Feb 12 00:08:26 CST 2015

反格式化指定格式日期:Thu Feb 12 00:18:44 CST 2015

//Thu Feb 12 00:18:44 CST 2015


设置时区显示:2015年2月11日 星期三 上午06时30分56秒 HST

//2015年2月11日 星期三 上午06时27分59秒 HST


设置英文显示:Feb 11, 2015

//Feb 12, 2015

设置中文显示:2015-2-11

//2015-2-12


姓名:ENIAC

//ENIAC

年龄:he is 34 years old!

//he is 34 years old!


姓名:ENIAC

//ENIAC

年龄:he is 34 years old!

//he is 34 years old!


8,744,645.654

//8,744,645.654

1,458,744,645.654

//1,458,744,645.654

1,458,744,645.7

//1,458,744,645.7

1458744645.7

//1458744645.7

14.587E8

//14.587E8


反格式化科学计数法:1458700000

//1458700000

反百分比:0.032

//0.032


集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。