jsp页面格式化数字或时间

转载自:http://blog.csdn.net/hakunamatata2008/archive/2011/01/21/6156203.aspx 
所有标签: 

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秒) 

 

保留两位小数 

 

格式数字(45,678.234) 

 

格式百分比(23%) 
 
  

其他 

:资源绑定。除了以前提到过的在web.xml中声明以外,还可以利用此标签。 

 

:设置locale,主要是用于这种情况,一个中国人在国外,locale是en_US,但想用中文显示。 

例: 

:输出properties文件中的指定内容。 

 

格式化普通数字 
格式化百分比 

三种数字类型参数:currency,number,percent 

 
分析出数字 

格式化文本编码 

 
type="both" 输入日期也同时输出具体时间 
timeStyle="long" 时间以“长”格式输出 差别:下午02时06分59秒 与 14:06:59 
dateStyle="long" 日期以“长”格式输出 差别:2006年9月7日 与 2006-9-7 

四种长短参数:long,short,medium,full 

时区偏移,与上面可配合使用: 
 

 
分析出时间 

具体例子: 

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? 

      
        test value:  
      



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

表示读取 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+"/";  
    %>  
      
      
        
          
          
          
        
        
        
          
            从 .properties 文件中读取最简单的信息输出:  
              
           


  
            从 .properties 文件中读取带有可填参数的信息,填入参数并输出:  
              
                  
                  
              
           
  

            数字 格式化并输出:
  

            数字:
  

              
            数字,定制了格式:
  

            货币:
  

            百分比:
  

           
  

            格式化日期:
  

              
           
  

           
  

           
  

           
  

            将字符串转化到正确的数字:
  

            忽略第一个不符合数字条件的字符和其之后的所有字符,如果字符串不是以数字开头则报错
  

            123.02a
  

            123
  

            123.00a1
  

            3saaa
  

              
          
              
        
      



jstl 包下的 jstl.properties 文件: 

view plaincopy to clipboardprint? 

    #for jstl learn  
    basemsg=This is a base msg.  
    msgwithparam=This is a msg with params:first {0} second {1} .   



另一篇: 

国际化格式标签库包括国际化,消息和数字日期格式化: 

(1) 国际化:  

如: 
<%@ 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" %> 
 
 
 
 
Insert title here 
 
 
 

中文-大陆: 

 

中文-台湾 

 

中文-新加坡 

 

英文: 
 
 
 



页面输出: 


中文-大陆: 2007-12-25 
中文-台湾 2007/12/25 
中文-新加坡 25-十二月-07 
英文: Dec 25, 2007 



(2)消息标签:  

如: 
<%@ 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" %> 
 
 
 
bundle test 
 
 
 
数据库驱动程序名:
 

连接字符串:
 

用户名:
 

密码: 
    
 

名字:
 

动态提示信息:
 

 

 
 
 
动态提示信息: 
 
  邓子云 
   
 

 

你可能感兴趣的:(jstl)