工程目录(建议第一次配置严格按照工程目录来创建文件):
创建资源文件(文件按照上面的目录结构来创建):
web.xml中添加国际化监听器
<!--国际化监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
web.xml中添加applicationContext-....xml的文件加载器;
<!-- 加载spring容器 将mapper service springmvc与spring关联起来-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
创建applicationContext-contextLoader.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 国际化资源配置,资源文件绑定器-->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 国际化资源文件配置,指定properties文件存放位置 以什么开头 -->
<property name="basename" value="classpath:contextLoader/language" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 动态切换国际化 ,国际化放在session中 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh_CN"></property>
</bean>
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 通过这个参数来决定获取那个配置文件 -->
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>
</beans>
创建前端页面contextLoader.jsp(注意添加spring标签库)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<!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>
选择语言:<a href="?langType=zh_CN">中文</a> | <a href="?langType=en_US">英文</a>
<br></br>
这里展示选择对应语言后的“你好”的翻译(前台标签翻译):
<spring:message code="hello" />
<br></br>
这里展示选择对应语言后的“欢迎你”的翻译(后台代码翻译):${welcome}
</body>
</html>
创建后端控制器:
/**
* 国际化
* @param langType
* @param model
* @param request
* @param response
* @return
*/
@RequestMapping("toContextLoader")
public String toContextLoader(@RequestParam(value="langType", defaultValue="zh_CN") String langType,Model model,Locale locale,HttpServletRequest request, HttpServletResponse response) {
String[] arr = language.split("_");
(new SessionLocaleResolver()).setLocale(request, response, new Locale(arr[0], arr[1]));
RequestContext req = new RequestContext(request);
model.addAttribute("welcome", req.getMessage("hello"));
return "contextLoader";
}