SpringMVC国际化的具体实现代码下载地址:http://download.csdn.net/download/bingbeichen/9798174。
1. 基本实现
目标1:在页面上能够根据浏览器的语言设置对文本(不是内容)、时间和数值进行本地化操作。
①. 在src目录下创建国际化资源文件,并添加需要国际化的键值对信息
## i18n_zh_CN.properties文件
i18n.username=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801
## i18n_en_US.properties文件
i18n.username=User
i18n.password=Password
②. 在SpringMVC的配置文件中,配置ResourceBundleMessageSource来加载国际化资源文件
<!-- 配置I18n -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
③. 在index.jsp页面发送请求信息,直接响应到success.jsp页面(需要使用JSTL的fmt标签来实现)
<!-- index.jsp页面 -->
<body>
<a href="testI18n">Test I18n</a>
</body>
<!-- springmvc.xml配置文件 -->
<mvc:view-controller path="/testI18n" view-name="success"/>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- success.jsp页面 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<fmt:message key="i18n.username"></fmt:message><br><br>
<fmt:message key="i18n.password"></fmt:message><br><br>
</body>
</html>
2. 初等实现
目标2:可以在处理器的目标方法中获取国际化资源文件Locale对应的消息。
①. 取消SpringMVC配置文件中,直接将testI18n请求响应到success.jsp页面的配置
②. 创建处理器,并创建目标方法,以拦截testIng8n请求
package com.qiaobc.springmvc.i18n;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestI18n {
@Autowired
private ResourceBundleMessageSource messageSource;
@RequestMapping("/testI18n")
public String testI18n(Locale locale) {
String username = messageSource.getMessage("i18n.username", null, locale);
String password = messageSource.getMessage("i18n.password", null, locale);
System.out.println(username + " : " + password);
return "success";
}
}
3. 终极实现
目标3:可以通过超链接切换Locale,而不再依赖于浏览器的语言设置情况。
①. 在index.jsp页面添加带有locale请求参数的超链接,参数名必须为locale
<!-- index.jsp页面 -->
<body>
<a href="testI18n">Test I18n</a><br><br>
<a href="testI18n?locale=zh_CN">Test I18n zh_CN</a><br><br>
<a href="testI18n?locale=en_US">Test I18n en_US</a><br><br>
</body>
②. 在SpringMVC配置文件中配置SessionLocaleResolver和LocaleChangeInterceptor拦截器
<!-- 配置SessionLocaleResolver -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<!-- 配置LocaleChangeInterceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
4. 本地化解析器和本地化拦截器
①. AcceptHeaderLocaleResolver: SpringMVC默认使用该解析器,即根据HTTP请求头的Accept-Language参数确定本地化类型;
②. CookieLocaleResolver:根据指定的Cookie值确定本地化类型;
③. SessionLocaleResolver:根据Session中特定的属性确定本地化类型;
④. LocaleChangeInteceptor:从请求参数中获取本地请求对应的本地化类型,参数名为locale。