国际化格式标签库包括国际化,消息和数字日期格式化:
(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 >
其对应的读取文件为dbconn.properties(当然是放在web-inf/classes下了),内容为:
#SQL Server driverName=com.microsoft.jdbc.sqlserver.SQLServerDriver connString=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDatabase userName=sa password=123456 name=邓佳容 messageTemp=myname is {0},today is {1,date}
其页面输出为:
数据库驱动程序名:com.microsoft.jdbc.sqlserver.SQLServerDriver 连接字符串:jdbc:microsoft:sqlserver: // localhost:1433;DatabaseName=testDatabase 用户名:sa 密码: 123456 名字:邓佳容 动态提示信息:myname is ... { 0 } ,today is ... { 1 ,date} 动态提示信息: myname is 邓子云,today is 2007 - 12 - 25
解释一下其中的几个标签:
<fmt:bundle>标签用于绑定数据源.properties文件;
<fmt:bundle basename="源文件名,且不能带后缀哦,如上例就可以了" prefix=""> 语句,代码等 </fmt:bundle>
<fmt::message>标签用于从指定的资源文件中把指定的键值取出来;
<fmt:message key ="" [var="varname"] [bundle=""] [scope="page|..."]/> 如果用到var的话就不会在页面直接输出,而需要用到<c:out>标签来进行页面的输出,如上例;
<fmt:message >标签可以配合<fmt:param>标签来进行设定<fmt:message >标签指向键的动态值,如上例;
<fmt:setBundle>标签用于设置默认的数据来源;
<fmt:setBundle>标签用来设置默认的数据来源; <fmt:setBundle basename="" [ var=""] [scope="" ] />
(3)
<fmt:formatNumber>标签用于根据设定的区域将数据格式化输出;
<fmt:formatDate>标签用于格式化输出日期和时间;
<fmt:parseDate>标签用于把字符串类型的日期和时间转换成日期型数据类型;
<fmt:setTimeZone>标签用于设定默认的时区;
<fmt:timeZone>标签用于设定在本签体内有效的时区;