jsp頁面使用標籤格式化數字或時間
阿新 • • 發佈:2019-02-07
Tags
fmt:requestEncoding
fmt:setLocale
fmt:timeZone
fmt:setTimeZone
fmt:bundle
fmt:setBundle
fmt:message
fmt:param
fmt:formatNumber
fmt:parseNumber
fmt:formatDate
fmt:parseDate
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
jstl fmt 函式大全
主要功能格式化
日期格式(2008年5月5日22點00分23秒)
<fmt:formatDate value="<%=new Date() %>" pattern="yyyy年MM月dd日HH點mm分ss秒" />
保留兩位小數
<fmt:formatNumber value="123.123456789" pattern="0.00"/>
格式數字(45,678.234)
<fmt:formatNumber type="number" value="45678.2345" />
格式百分比(23%)
<fmt:formatNumber type="percent" value="0.2345" />
<fmt:formatNumber value="${item.DD_NUM/item.TOL_NUM}" type="number" pattern="0.00%" />
其他
<fmt:bundle>:資源繫結。除了以前提到過的在web.xml中宣告以外,還可以利用此標籤。
例<fmt:bundle basename="message"></fmt:bundle>
<fmt:setLocale>:設定locale,主要是用於這種情況,一箇中國人在國外,locale是en_US,但想用中文顯示。
例:<fmt:setLocal value="zh_CN"/>
<fmt:message>:輸出properties檔案中的指定內容。
例<fmt:message key="user"/>
<fmt:formatNumber type="number">格式化普通數字
<fmt:formatNumber type="percent">格式化百分比
三種數字型別引數:currency,number,percent
<fmt:parseNumber var="i" type="number" value="45678.2345" />
<c:out value="${i}" escapeXml="false" /> 分析出數字
<fmt:requestEncoding value="GB18030"/> 格式化文字編碼
<fmt:formatDate value="${date}" type="both" timeStyle="long" dateStyle="long" />
type="both" 輸入日期也同時輸出具體時間
timeStyle="long" 時間以“長”格式輸出 差別:下午02時06分59秒 與 14:06:59
dateStyle="long" 日期以“長”格式輸出 差別:2006年9月7日 與 2006-9-7
四種長短引數:long,short,medium,full
<fmt:timeZone value="${timezone}"/> 時區偏移,與上面可配合使用:
<fmt:formatDate value="${d}" timeZone="${zn}" type="both" />
<fmt:parseDate var="i" type="date" value="2006-12-11" />
<c:out value="${i}" escapeXml="false" /> 分析出時間
具體例子:
1)匯入jstl 包,載入ftm標籤
首先將jstl的jar包放入類庫中,使用1.2版本
其次在jsp檔案中引入所需要的 標記庫,對於 ftm 標籤,如下:
view plaincopy to clipboardprint?
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>
2)輸出 .properties 檔案中的資訊
view plaincopy to clipboardprint?
<fmt:bundle basename="fmt">
test value:<fmt:message key="test" />
</fmt:bundle>
其中 <fmt:bundle basename="fmt"> 指定了資原始檔的位置,例如: fmt 表示類根路徑下的 fmt.properties 檔案,my.fmt 表示 包my下的ftm.properties檔案;
<fmt:message key="test" />表示讀取 key為test的值,並輸出;
3)給出1個例子,包含許多標籤的使用
fmt.jsp:
view plaincopy to clipboardprint?
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix='c' uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
<mce:style type="text/css"><!--
body {background-color: black;color: white;}
span {text-align: center;color: green;background-color: yellow;}
.notice {color: rgb(250,37,62);}
hr { background-color: fuchsia; height: 5px;}
--></mce:style><style type="text/css" mce_bogus="1"> body {background-color: black;color: white;}
span {text-align: center;color: green;background-color: yellow;}
.notice {color: rgb(250,37,62);}
hr { background-color: fuchsia; height: 5px;}
</style>
</head>
<body>
<fmt:bundle basename="jstl.jstl">
<span>從 .properties 檔案中讀取最簡單的資訊輸出:</span>
<fmt:message key="basemsg" />
<hr/>
<span>從 .properties 檔案中讀取帶有可填引數的資訊,填入引數並輸出:</span>
<fmt:message key="msgwithparam">
<span class="notice"><fmt:param value="param-1-value" />
<span class="notice"><fmt:param value="param-2-value" />
</fmt:message>
<hr/>
<span>數字 格式化並輸出:</span><br/>
數字:<fmt:formatNumber value="1234567890" type="number"/><br/>
<!-- 定製數字格式時,# 表示按照預設格式來, -->
數字,定製了格式:<fmt:formatNumber value="1234567890" type="number" pattern="#,#00.0#" /><br/>
貨幣:<fmt:formatNumber value="35000" type="currency" /><br/>
百分比:<fmt:formatNumber value="0.317" type="percent" /><br/>
<hr/>
<span>格式化日期:</span><br/>
<jsp:useBean id="now" class="java.util.Date"></jsp:useBean>
<fmt:formatDate value="${now}" type="date" /><br/>
<fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long" /><br/>
<fmt:formatDate value="${now}" type="both" pattern="yyyy.MM.dd HH:mm:ss" /><br/>
<hr/>
<span>將字串轉化到正確的數字:<br/>
忽略第一個不符合數字條件的字元和其之後的所有字元,如果字串不是以數字開頭則報錯</span><br/>
<fmt:parseNumber type="number" >123.02a</fmt:parseNumber><br/>
<fmt:parseNumber type="number" pattern="#,#00.0#">123</fmt:parseNumber><br/>
<fmt:parseNumber type="number" pattern="#,#00.0#">123.00a1</fmt:parseNumber><br/>
<fmt:parseNumber type="number" pattern="#,#00.0#">3saaa</fmt:parseNumber><br/>
</fmt:bundle>
</body>
</html>
jstl 包下的 jstl.properties 檔案:
view plaincopy to clipboardprint?
#for jstl learn
basemsg=This is a base msg.
msgwithparam=This is a msg with params:first <font color="red">{0}</font> second <font color="aqua"> {1}</font> .
另一篇:
國際化格式標籤庫包括國際化,訊息和數字日期格式化:
(1) 國際化:<fmt:setLocale> <fmt::requestEncoding>
如:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:set var="todayValue" value="<%=new Date() %>"/>
中文-大陸:<fmt:setLocale value="zh"/>
<fmt:formatDate value="${todayValue}"/><br>
中文-臺灣<fmt:setLocale value="zh_tw"/>
<fmt:formatDate value="${todayValue}"/><br>
中文-新加坡<fmt:setLocale value="zh_sg"/>
<fmt:formatDate value="${todayValue}"/><br>
英文:<fmt:setLocale value="en"/>
<fmt:formatDate value="${todayValue}"/>
</body>
</html>
頁面輸出:
中文-大陸: 2007-12-25
中文-臺灣 2007/12/25
中文-新加坡 25-十二月-07
英文: Dec 25, 2007
(2)訊息標籤:<fmt:bundle> <fmt:message> <fmt:setBundle> <fmt:param>
如:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>bundle test</title>
</head>
<body>
<fmt:bundle basename="dbconn">
資料庫驅動程式名:<fmt:message key="driverName"/><br>
連線字串:<fmt:message key="connString"/><br>
使用者名稱:<fmt:message key="userName"/><br>
密碼:<fmt:message key="password" var="password"/>
<c:out value="${password}"/><br>
名字:<fmt:message key="name"/><br>
動態提示資訊:<fmt:message key="messageTemp"/><br>
</fmt:bundle>
<!-- 修改.properties檔案中某個鍵的動態值 -->
<c:set var="todayTemp" value="<%=new Date() %>"/>
<fmt:setBundle basename="dbconn"/>
動態提示資訊:
<fmt:message key="messageTemp">
<fmt:param>鄧子云</fmt:param>
<fmt:param value="${todayTemp}"></fmt:param>
</fmt:message>
</body>
</html>
fmt:requestEncoding
fmt:setLocale
fmt:timeZone
fmt:setTimeZone
fmt:bundle
fmt:setBundle
fmt:message
fmt:param
fmt:formatNumber
fmt:parseNumber
fmt:formatDate
fmt:parseDate
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
jstl fmt 函式大全
主要功能格式化
日期格式(2008年5月5日22點00分23秒)
<fmt:formatDate value="<%=new Date() %>" pattern="yyyy年MM月dd日HH點mm分ss秒" />
保留兩位小數
<fmt:formatNumber value="123.123456789" pattern="0.00"/>
格式數字(45,678.234)
<fmt:formatNumber type="number" value="45678.2345" />
格式百分比(23%)
<fmt:formatNumber type="percent" value="0.2345" />
<fmt:formatNumber value="${item.DD_NUM/item.TOL_NUM}" type="number" pattern="0.00%" />
其他
<fmt:bundle>:資源繫結。除了以前提到過的在web.xml中宣告以外,還可以利用此標籤。
例<fmt:bundle basename="message"></fmt:bundle>
<fmt:setLocale>:設定locale,主要是用於這種情況,一箇中國人在國外,locale是en_US,但想用中文顯示。
例:<fmt:setLocal value="zh_CN"/>
<fmt:message>:輸出properties檔案中的指定內容。
例<fmt:message key="user"/>
<fmt:formatNumber type="number">格式化普通數字
<fmt:formatNumber type="percent">格式化百分比
三種數字型別引數:currency,number,percent
<fmt:parseNumber var="i" type="number" value="45678.2345" />
<c:out value="${i}" escapeXml="false" /> 分析出數字
<fmt:requestEncoding value="GB18030"/> 格式化文字編碼
<fmt:formatDate value="${date}" type="both" timeStyle="long" dateStyle="long" />
type="both" 輸入日期也同時輸出具體時間
timeStyle="long" 時間以“長”格式輸出 差別:下午02時06分59秒 與 14:06:59
dateStyle="long" 日期以“長”格式輸出 差別:2006年9月7日 與 2006-9-7
四種長短引數:long,short,medium,full
<fmt:timeZone value="${timezone}"/> 時區偏移,與上面可配合使用:
<fmt:formatDate value="${d}" timeZone="${zn}" type="both" />
<fmt:parseDate var="i" type="date" value="2006-12-11" />
<c:out value="${i}" escapeXml="false" /> 分析出時間
具體例子:
1)匯入jstl 包,載入ftm標籤
首先將jstl的jar包放入類庫中,使用1.2版本
其次在jsp檔案中引入所需要的 標記庫,對於 ftm 標籤,如下:
view plaincopy to clipboardprint?
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>
2)輸出 .properties 檔案中的資訊
view plaincopy to clipboardprint?
<fmt:bundle basename="fmt">
test value:<fmt:message key="test" />
</fmt:bundle>
其中 <fmt:bundle basename="fmt"> 指定了資原始檔的位置,例如: fmt 表示類根路徑下的 fmt.properties 檔案,my.fmt 表示 包my下的ftm.properties檔案;
<fmt:message key="test" />表示讀取 key為test的值,並輸出;
3)給出1個例子,包含許多標籤的使用
fmt.jsp:
view plaincopy to clipboardprint?
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix='c' uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
<mce:style type="text/css"><!--
body {background-color: black;color: white;}
span {text-align: center;color: green;background-color: yellow;}
.notice {color: rgb(250,37,62);}
hr { background-color: fuchsia; height: 5px;}
--></mce:style><style type="text/css" mce_bogus="1"> body {background-color: black;color: white;}
span {text-align: center;color: green;background-color: yellow;}
.notice {color: rgb(250,37,62);}
hr { background-color: fuchsia; height: 5px;}
</style>
</head>
<body>
<fmt:bundle basename="jstl.jstl">
<span>從 .properties 檔案中讀取最簡單的資訊輸出:</span>
<fmt:message key="basemsg" />
<hr/>
<span>從 .properties 檔案中讀取帶有可填引數的資訊,填入引數並輸出:</span>
<fmt:message key="msgwithparam">
<span class="notice"><fmt:param value="param-1-value" />
<span class="notice"><fmt:param value="param-2-value" />
</fmt:message>
<hr/>
<span>數字 格式化並輸出:</span><br/>
數字:<fmt:formatNumber value="1234567890" type="number"/><br/>
<!-- 定製數字格式時,# 表示按照預設格式來, -->
數字,定製了格式:<fmt:formatNumber value="1234567890" type="number" pattern="#,#00.0#" /><br/>
貨幣:<fmt:formatNumber value="35000" type="currency" /><br/>
百分比:<fmt:formatNumber value="0.317" type="percent" /><br/>
<hr/>
<span>格式化日期:</span><br/>
<jsp:useBean id="now" class="java.util.Date"></jsp:useBean>
<fmt:formatDate value="${now}" type="date" /><br/>
<fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long" /><br/>
<fmt:formatDate value="${now}" type="both" pattern="yyyy.MM.dd HH:mm:ss" /><br/>
<hr/>
<span>將字串轉化到正確的數字:<br/>
忽略第一個不符合數字條件的字元和其之後的所有字元,如果字串不是以數字開頭則報錯</span><br/>
<fmt:parseNumber type="number" >123.02a</fmt:parseNumber><br/>
<fmt:parseNumber type="number" pattern="#,#00.0#">123</fmt:parseNumber><br/>
<fmt:parseNumber type="number" pattern="#,#00.0#">123.00a1</fmt:parseNumber><br/>
<fmt:parseNumber type="number" pattern="#,#00.0#">3saaa</fmt:parseNumber><br/>
</fmt:bundle>
</body>
</html>
jstl 包下的 jstl.properties 檔案:
view plaincopy to clipboardprint?
#for jstl learn
basemsg=This is a base msg.
msgwithparam=This is a msg with params:first <font color="red">{0}</font> second <font color="aqua"> {1}</font> .
另一篇:
國際化格式標籤庫包括國際化,訊息和數字日期格式化:
(1) 國際化:<fmt:setLocale> <fmt::requestEncoding>
如:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:set var="todayValue" value="<%=new Date() %>"/>
中文-大陸:<fmt:setLocale value="zh"/>
<fmt:formatDate value="${todayValue}"/><br>
中文-臺灣<fmt:setLocale value="zh_tw"/>
<fmt:formatDate value="${todayValue}"/><br>
中文-新加坡<fmt:setLocale value="zh_sg"/>
<fmt:formatDate value="${todayValue}"/><br>
英文:<fmt:setLocale value="en"/>
<fmt:formatDate value="${todayValue}"/>
</body>
</html>
頁面輸出:
中文-大陸: 2007-12-25
中文-臺灣 2007/12/25
中文-新加坡 25-十二月-07
英文: Dec 25, 2007
(2)訊息標籤:<fmt:bundle> <fmt:message> <fmt:setBundle> <fmt:param>
如:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>bundle test</title>
</head>
<body>
<fmt:bundle basename="dbconn">
資料庫驅動程式名:<fmt:message key="driverName"/><br>
連線字串:<fmt:message key="connString"/><br>
使用者名稱:<fmt:message key="userName"/><br>
密碼:<fmt:message key="password" var="password"/>
<c:out value="${password}"/><br>
名字:<fmt:message key="name"/><br>
動態提示資訊:<fmt:message key="messageTemp"/><br>
</fmt:bundle>
<!-- 修改.properties檔案中某個鍵的動態值 -->
<c:set var="todayTemp" value="<%=new Date() %>"/>
<fmt:setBundle basename="dbconn"/>
動態提示資訊:
<fmt:message key="messageTemp">
<fmt:param>鄧子云</fmt:param>
<fmt:param value="${todayTemp}"></fmt:param>
</fmt:message>
</body>
</html>