jstl fmt标签的使用

来源:互联网 发布:个性化排序算法 编辑:程序博客网 时间:2024/04/19 19:15

所有标签:

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: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 标签,如下:

[java] view plaincopy
  1. <%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>  

2)输出 .properties 文件中的信息

[java] view plaincopy
  1. <fmt:bundle basename="fmt">  
  2.     test value:<fmt:message key="test" />  
  3. </fmt:bundle>  

其中 <fmt:bundle basename="fmt"> 指定了资源文件的位置,例如: fmt 表示类根路径下的 fmt.properties 文件,my.fmt 表示 包my下的ftm.properties文件;

<fmt:message key="test" />表示读取 key为test的值,并输出;

3)给出1个例子,包含许多标签的使用

fmt.jsp:

[java] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. <%@ taglib prefix='c' uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.     <!--  
  13.     <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">  
  14.     -->  
  15.     <mce:style type="text/css"><!--  
  16.         body {background-color: black;color: white;}  
  17.         span {text-align: center;color: green;background-color: yellow;}  
  18.         .notice {color: rgb(250,37,62);}  
  19.         hr { background-color: fuchsia; height: 5px;}  
  20.       
  21. --></mce:style><style type="text/css" mce_bogus="1">     body {background-color: black;color: white;}  
  22.         span {text-align: center;color: green;background-color: yellow;}  
  23.         .notice {color: rgb(250,37,62);}  
  24.         hr { background-color: fuchsia; height: 5px;}  
  25.     </style>  
  26.   </head>  
  27.     
  28.   <body>  
  29.     <fmt:bundle basename="jstl.jstl">  
  30.         <span>从 .properties 文件中读取最简单的信息输出:</span>  
  31.         <fmt:message key="basemsg" />  
  32.         <hr/>  
  33.         <span>从 .properties 文件中读取带有可填参数的信息,填入参数并输出:</span>  
  34.         <fmt:message key="msgwithparam">  
  35.             <span class="notice"><fmt:param value="param-1-value" />  
  36.             <span class="notice"><fmt:param value="param-2-value" />  
  37.         </fmt:message>  
  38.         <hr/>  
  39.         <span>数字 格式化并输出:</span><br/>  
  40.         数字:<fmt:formatNumber value="1234567890" type="number"/><br/>  
  41.         <!-- 定制数字格式时,# 表示按照默认格式来, -->  
  42.         数字,定制了格式:<fmt:formatNumber value="1234567890" type="number" pattern="#,#00.0#" /><br/>  
  43.         货币:<fmt:formatNumber value="35000" type="currency" /><br/>  
  44.         百分比:<fmt:formatNumber value="0.317" type="percent" /><br/>  
  45.         <hr/>  
  46.         <span>格式化日期:</span><br/>  
  47.         <jsp:useBean id="now" class="java.util.Date"></jsp:useBean>  
  48.         <fmt:formatDate  value="${now}" type="date" /><br/>  
  49.         <fmt:formatDate  value="${now}" type="both" dateStyle="long" timeStyle="long" /><br/>  
  50.         <fmt:formatDate  value="${now}" type="both" pattern="yyyy.MM.dd HH:mm:ss" /><br/>  
  51.         <hr/>  
  52.         <span>将字符串转化到正确的数字:<br/>  
  53.         忽略第一个不符合数字条件的字符和其之后的所有字符,如果字符串不是以数字开头则报错</span><br/>  
  54.         <fmt:parseNumber type="number" >123.02a</fmt:parseNumber><br/>  
  55.         <fmt:parseNumber type="number" pattern="#,#00.0#">123</fmt:parseNumber><br/>  
  56.         <fmt:parseNumber type="number" pattern="#,#00.0#">123.00a1</fmt:parseNumber><br/>  
  57.         <fmt:parseNumber type="number" pattern="#,#00.0#">3saaa</fmt:parseNumber><br/>  
  58.           
  59.     </fmt:bundle>  
  60.           
  61.   </body>  
  62. </html>  

jstl 包下的 jstl.properties 文件:

[java] view plaincopy
  1. #for jstl learn  
  2. basemsg=This is a base msg.  
  3. 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