jstl的標籤

今天小仲看了一些jstl的標籤,由於之前只是用了一點用來接收數據的普通標籤和幾個簡單的<c:foreach>,今天看了一些其他其它的jstl標籤。特此分享於此!!

 

<%@ 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:parseDate value="2008-8-8" pattern="yyyy-MM-dd" var = "abccba" scope="session" />

保留兩位小數

<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: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" /> 分析出時間

JSTL fmt:formatNumber 數字、貨幣格式化

<fmt:formatNumber value="12" type="currency" pattern="$.00"/> -- $12.00

<fmt:formatNumber value="12" type="currency" pattern="$.0#"/> -- $12.0

<fmt:formatNumber value="1234567890" type="currency"/> -- $1,234,567,890.00(那個貨幣的符號和當前web服務器的 local 設定有關)

<fmt:formatNumber value="123456.7891" pattern="#,#00.0#"/> -- 123,456.79

<fmt:formatNumber value="123456.7" pattern="#,#00.0#"/> -- 123,456.7

<fmt:formatNumber value="123456.7" pattern="#,#00.00#"/> -- 123,456.70

<fmt:formatNumber value="12" type="percent" /> -- 1,200% (type 可以是currency、 number、 和percent)。

java格式化輸出:
DecimalFormat df = new DecimalFormat("格式");
String fmt =df.format(double);
符號                  意義
0                     一個數位
#                     一個數位,前導零和追尾零不顯示
.                      小數點分割位置
,                     組分隔符的位置
-                      負數前綴
%                    用100乘,並顯示百分號
其他任何符號    在輸出字符串中包括指定符號


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章