JSTL 标签支持国际化的标签为
<fmt:bundle> <fmt:message> <fmt:setBundle><fmt:param>
<fmt:bundle> 功能:指定消息资源使用的文件
<fmt:message>功能:显示消息资源文件中指定key的消息,支持带参数消息
<fmt:param> 功能:给带参数的消息置参数值
<fmt:setBundle> 功能:设置消息资源文件
1、引入下面两个标签
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
2、准备两个语言资源存放在classes目录下面,也就是src目录下。
message_en_US.properties文件
hello=english{0}
message_zh_CN.properties文件
hello=hello1\u4F60\u597D{0}
3、设置本地的环境
<fmt:setLocale value="zh_CN"/>
4、资源文件的绑定
<fmt:setBundle basename="message" var="myBundle" />
备注: <fmt:bundle> 标签将资源配置文件绑定于它标签体中的显示, <fmt:setBundle> 标签则允许将资源配置文件保存为一个变量,在之后的工作可以根据该变量来进行。
<fmt:setBundle basename="applicationMessage" var="applicationBundle"/>
<fmt:bundle basename="applicationAllMessage">
<fmt:message key="userName" />
<p>
<fmt:message key="passWord" bundle="${applicationBundle}" />
</fmt:bundle>
5、显示文件信息内容
<fmt:message key="hello" bundle="${myBundle }">
<fmt:param>niii</fmt:param>
</fmt:message>
备注:<fmt:param>niii</fmt:param>里面的参数对应hello这个名字的变量,从0开始计算
<%@ 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" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript" src="<%=request.getContextPath()%>/js/util/jquery-1.4.4.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <fmt:setLocale value="zh_CN"/> <fmt:setBundle basename="message" var="myBundle" /> <fmt:message key="hello" bundle="${myBundle }"> <fmt:param>niii</fmt:param> </fmt:message> </body> </html>