首先,jstl既然可以国际化,那么必然可以自动根据local设置来选择资源文件。
2,fmt:setLocal 可以设置Local,从而改变输出格式。
3,fmt:message 可以输出资源文件里的key对应的value。
<fmt:message key=xxx"/>
和struts一样 还可以传参数
<fmt:message key=xxx"/>
<fmt:param value="${abc}"/>
</fmt:message>
4,<fmt:bundle>
<fmt:setBundle>
这地方有点意思,首先我在工程的WEB-INF/classes下面建立了一个资源文件resources.properties。然后在jsp页面里
<fmt:bundle basename="resources.properties">使用此资源文件。
Nitrox插件提示找不到resources.properties的警告.
使用<fmt:message key="xxx"/>也无法显示.
原因:工程没有指定input ,output的对应关系.这样classes并不是classpath路径.
解决办法:
设置input为/WEB-INF/src ,output /WEB-INF/classes
然后把resources.properties放在 /WEB-INF/src/下面。
这样在/WEB-INF/classes/下面自动生成了一个resources.properties.这就是我想要的。
好了,在试一次,资源文件找到了。
<fmt:message key="xxx"/>也能正常显示了。
5,难道<fmt:message>必须和<fmt:bundle >搭配使用才行吗?实在觉得有点罗嗦。
没办法,jstl就是这样用的。
6,jstl使用资源文件有个大的问题,因为fmt:bundle basename="xxx" 指定死了资源文件了,那么如果local不同了
岂不是还从这个资源文件里取数据吗?这样美国的网页浏览仍旧显示日文,就不合理了.
我理解错了,<fmt:bundle basename="xxx"/>并非指定资源文件就是他,而是指资源文件的基本名字,例如,
如果是英国的local那么自动查找xxx_en.properties,如果是中国的local那么自动去查找xxx_zh.properties.
和struts是一样的.
<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" /> 分析出时间
日期表示
<fmt:formatDate value="${DATE1}" pattern="yyyy-MM-dd hh:mm:ss" type="date" dateStyle="long" />
百分数,千分数表示
<fmt:formatNumber value="${DoubleVALUE}" type="number" pattern="0.00‰" /> 0.01 ‰<fmt:formatNumber value="${DoubleVALUE}" type="number" pattern="0.00%" /> 0.20 %
其他数字表示
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。
货币表示
===1
<fmt:setLocale value="ch_CH" />
<fmt:formatNumber value="${data}" type="currency" />
==2
<fmt:formatNumber value="${doubleValue}" type="number" pattern="¥0.00" />
参考 ==============================================================================
java格式化输出:
DecimalFormat df = new DecimalFormat("格式");
String fmt =df.format(double);
符号 意义
0 一个数位
# 一个数位,前导零和追尾零不显示
. 小数点分割位置
, 组分隔符的位置
- 负数前缀
% 用100乘,并显示百分号
其他任何符号 在输出字符串中包括指定符号
==================================================================================
<%@ page language="java" contentType="text/html; charset=gb18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>My JSP 'fmt.jsp' starting page</title>
</head>
<body>
<c:set var="salary" value="3540.2301"/>
<c:set var="total" value="56225.2301"/>
<fmt:setLocale value="en_US"/>
currency:<fmt:formatNumber value="${salary}" type="currency" currencyCode="USD"/><br>
percent:<fmt:formatNumber value="${salary/total}" type="percent" maxFractionDigits="4"/><br>
<hr>
<jsp:useBean id="now" class="java.util.Date"></jsp:useBean>
<fmt:setLocale value="zh_CN"/>
full--><fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/><br>
long--><fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long"/><br>
medium--><fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="medium"/><br>
default--><fmt:formatDate value="${now}" type="both" dateStyle="default" timeStyle="default"/><br>
short--><fmt:formatDate value="${now}" type="both" dateStyle="short" timeStyle="short"/><br>
</body>
</html>
=================================================================================
JSP 国际化-格式化货币和日期
1.格式化货币
世界上许多国家都有不同的货币格式和数字格式惯例。针对特定的本地化环境正确地格式化和显示货币是本地化的一个重要部分。
<%@ page 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" %>
<html>
<head>
<title>Currency Formatting</title>
</head>
<body>
<h1>Currency Formatting and locales</h1>
<h3>English, Great Britain</h3>
<fmt:setLocale value="en_GB" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>English, USA</h3>
<fmt:setLocale value="en_US" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>French, France</h3>
<fmt:setLocale value="fr_FR" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Japanese, Japan</h3>
<fmt:setLocale value="ja_JP" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Korean, Korea</h3>
<fmt:setLocale value="ko_KR" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Spanish, Spain</h3>
<fmt:setLocale value="es_ES" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Arabic, Egypt</h3>
<fmt:setLocale value="ar_EG" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Using Local Numeric Formatting for Different Currency</h3>
<h4>English, Great Britan</h4>
<fmt:setLocale value="en_GB" />
<fmt:formatNumber type="currency" value="80000" /><br/>
<fmt:formatNumber type="currency" value="80000" currencyCode="EUR"/><br/>
</body>
</html>
2.格式化日期
类似于数字和货币格式化,本地化环境还会影响生成日期和时间的方式。
<%@ page 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" %>
<html>
<head>
<title>Date Formatting</title>
</head>
<body>
<h1>Date Formatting and locale</h1>
<fmt:timeZone value="EST">
<jsp:useBean id="currentTime" class="java.util.Date"/>
<h3>English, Great Britain</h3>
<fmt:setLocale value="en_GB" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
<h3>English, USA</h3>
<fmt:setLocale value="en_US" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
<h3>French, France</h3>
<fmt:setLocale value="fr_FR" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
<h3>Japanese, Japan</h3>
<fmt:setLocale value="ja_JP" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
<h3>Korean, Korea</h3>
<fmt:setLocale value="ko_KR" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
<h3>Spanish, Spain</h3>
<fmt:setLocale value="es_ES" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
<h3>Arabic, Egypt</h3>
<fmt:setLocale value="ar_EG" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>
</fmt:timeZone>
</body>
</html>
<fmt:formatDate>动作的属性
type: 可以是time,date或both。控制是否只生成时间,只生成日期,或者时间日期都生成。
dateStyle: 可以是short, medium, long 或 full(default)。控制打印日期使用的具体格式。
timeStyle: 可以是short, medium, long 或 full(default)。控制打印时间使用的具体格式。
value: 这是一个java.util.Date 类型的值,用于生成日期和时间