JSTL中format标签的使用



 web.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <jsp-config>  
  8.     <taglib>  
  9.         <taglib-uri>jstl-c</taglib-uri>  
  10.         <taglib-location>/WEB-INF/c.tld</taglib-location>  
  11.     </taglib>  
  12.     <taglib>  
  13.         <taglib-uri>jstl-fmt</taglib-uri>  
  14.         <taglib-location>/WEB-INF/fmt.tld</taglib-location>  
  15.     </taglib>  
  16.     <taglib>  
  17.         <taglib-uri>jstl-fn</taglib-uri>  
  18.         <taglib-location>/WEB-INF/fn.tld</taglib-location>  
  19.     </taglib>  
  20.   </jsp-config>  
  21.     
  22.   <welcome-file-list>  
  23.     <welcome-file>index.jsp</welcome-file>  
  24.   </welcome-file-list>  
  25. </web-app>  
 taglib.jsp
Html代码   收藏代码
  1. <%@taglib uri="jstl-c" prefix="c" %>  
  2. <%@taglib uri="jstl-fmt" prefix="fmt" %>  
  3. <%@taglib uri="jstl-fn" prefix="fn" %>  
 设置语言环境的标签setLocale
Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@include file="taglib.jsp" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>ftm:setLoacle标记的使用</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.   
  21.   </head>  
  22.     
  23.   <body>  
  24.     <h2 align="center">用于指定所使用的语言环境</h2><br>  
  25.     <h3>value: 语言环境的字符串,或java.util.Locale的对象</h3><br>  
  26.     <h3>scope: 指定作用的范围</h3><br>  
  27.     <h3>variant: 针对特定的平台或供应商定制语言环境</h3><br>  
  28.     <br><br>  
  29.     <h2>执行下面的代码,将忽略用户浏览器设置中所指定的语言</h2>  
  30.     <fmt:setLocale value="zh_CN" scope="session"/>  
  31.   </body>  
  32. </html>  
 设置时区setTimeZone
Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@include file="taglib.jsp" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>fmt:setTimeZone标记的使用</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.   
  21.   </head>  
  22.     
  23.   <body>  
  24.     <h2 align="center">设置时区的标记</h2>  
  25.     <h3>value: 时区名或java.util.TimeZone对象</h3>  
  26.     <h3>scope: 作用的范围</h3>  
  27.     <h3>var: 保存TimeZone对象的变量的名称</h3>  
  28.     <fmt:setTimeZone value="GMT+8" scope="session" />  
  29.     <fmt:formatDate type="both" value="<%=new java.util.Date() %>"/><br>  
  30.   </body>  
  31. </html>  
 另一个设置时区的timeZone
Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@include file="taglib.jsp" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>fmt:timeZone标记的使用</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.   
  21.   </head>  
  22.     
  23.   <body>  
  24.     <h2 align="center">设置时区的标记,只对所包含的内容有效</h2><br>  
  25.     <fmt:formatDate type="both" value="<%=new java.util.Date() %>"/><br>  
  26.     <fmt:timeZone value="GMT+8">  
  27.         <fmt:formatDate type="both" value="<%=new java.util.Date() %>"/>  
  28.     </fmt:timeZone>  
  29.   </body>  
  30. </html>  
 formateDate
Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@include file="taglib.jsp" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>fmt:formateDate标记的使用</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.   
  21.   </head>  
  22.     
  23.   <body>  
  24.     <h2 align="center">格式化和显示日期</h2><br>  
  25.     <h3>value: 指定日期和时间</h3>  
  26.     <h3>type: 设置显示的是时间还是日期,有效取值是time,date或both,默认的是date</h3>  
  27.     <h3>dateStyle: 设置如何格式化日期信息。有效值是:default,short,medium,long和full</h3>  
  28.     <h3>timeStyle: 设置如何格式化时间信息。有效值是:default,short,medium,long和full</h3>  
  29.     <h3>pattern: 日期和时间的样式yy-MM-dd HH:mm:ss</h3>  
  30.     <h3>timeZone: 设置显示那个时区的日期和时间,要是没有就使用fmt:timeZone设置的</h3>  
  31.     <h3>var: 保存格式化的时间或日期,要是没这属性就输出时间或日期的结果</h3>  
  32.     <h3>scope: 作用的范围</h3>  
  33.       
  34.     <fmt:formatDate type="both" value="<%=new java.util.Date() %>" pattern="yy-MM-dd HH:mm:ss"/><br>  
  35.       
  36.     <fmt:timeZone value="GMT+8">  
  37.         <fmt:formatDate type="both" value="<%=new java.util.Date() %>" />  
  38.     </fmt:timeZone>  
  39.   </body>  
  40. </html>  
 formateNumber
Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@include file="taglib.jsp" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>fmt:formateNumber标记的使用</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.   
  21.   </head>  
  22.     
  23.   <body>  
  24.     <h2 align="center">格式化和显示数字</h2><br>  
  25.     <h3>value: 指定格式化的数值</h3>  
  26.     <h3>type: 指明要对哪种类型的数值进行格式化,有效取值是number,currency和percent</h3>  
  27.     <h3>pattern: 数字格式化的样式</h3>  
  28.     <h3>groupingUsed: 设置是否要对小数点前面的数字进行分组。例如,将较大数的每三个数字分为一组</h3>  
  29.     <h3>var: 保存格式化的数字的名称,要是没这属性就直接在页面输出</h3>  
  30.     <h3>scope: 作用的范围</h3>  
  31.       
  32.     <c:set value="1234567890.12345678" var="number" />  
  33.      默认方式的格式化数字<fmt:formatNumber type="number" value="${number}" /><br>  
  34.      数字不分组<fmt:formatNumber type="number" groupingUsed="false" value="${number}" /><br>  
  35.      整数部分最大三位<fmt:formatNumber type="number" maxIntegerDigits="3" value="${number}" /><br>  
  36.      整数部分最小取12位<fmt:formatNumber type="number" minIntegerDigits="12" value="${number}" /><br>  
  37.      小数部分最大取2位<fmt:formatNumber type="number" maxFractionDigits="2" value="${number}" /><br>  
  38.      小数部分最小取5位<fmt:formatNumber type="number" minFractionDigits="5" value="${number}" /><br>  
  39.      格式化货币数字<fmt:formatNumber type="currency" value="${number}" /><br>  
  40.      格式化百分比数字<fmt:formatNumber type="percent" value=".348" /><br>  
  41.   </body>  
  42. </html>  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值