概述
我们继续分析学习Spring MVC LocaleResolver,本节我们分析使用的是SessionLocaleResolver。
SessionLocaleResolver与CookieLocaleResolver类似,运用用户会话(session)实现LocaleResolver功能。
解析器(SessionLocaleResolver)
SessionLocaleResolver类继承关系如下图:
- AbstractLocaleResolver抽象类,实现LocaleResolver,在该抽象类中定义默认的Locale属性
- LocaleContextResolver接口,继承LocaleResolver,增加了TimeZone操作
- AbstractLocaleContextResolver抽象类,继承AbstractLocaleResolver类并实现LocaleContextResolver接口,定义了默认的TimeZone属性
- SessionLocaleResolver实现类,继承AbstractLocaleContextResolver类,即完成操作Locale和TImeZone的功能
SessionLocaleResolver类允许从用户请求会话中获取Locale和TimeZone,和CookieLocaleResolver对比,该策略在Servlet容器的HttpSession中存储客户端使用Locale等设置,这是设置对于每个会话(session)都是临时的,会话终止时信息丢失。
入口是resolveLocaleContext(final HttpServletRequest request)方法,即Spring MVC接收到客户端请求后,如果配置了SessionLocaleResolver,会调用此方法,源码如下:
// 解析Locale等信息方法
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
// 返回Locale和TimeZone
return new TimeZoneAwareLocaleContext() {
@Override
public Locale getLocale() {
// 从请求的会话中返回Locale
Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
if (locale == null) {
locale = determineDefaultLocale(request);
}
return locale;
}
@Override
@Nullable
public TimeZone getTimeZone() {
// 从请求的会话中返回TimeZone
TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
if (timeZone == null) {
timeZone = determineDefaultTimeZone(request);
}
return timeZone;
}
};
}
// 设置Locale和TimeZone
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
@Nullable LocaleContext localeContext) {
Locale locale = null;
TimeZone timeZone = null;
if (localeContext != null) {
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
// 把Locale设置到session中
WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
// 把TimeZone设置到session中
WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
实战
- 项目结构
参考https://segmentfault.com/a/1190000014797899中的项目结构,本章与其一致。
- 配置文件
在Spring MVC配置文件中配置资源加载以及SessionLocaleResolver Bean,配置如下:
<!-- 国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 如果资源文件放在classpath下,basename的value必须有classpath:前缀,否则报错:No message found under code... -->
<property name="basename" value="classpath:i18n/messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称返回 -->
<property name="useCodeAsDefaultMessage" value="true" />
<!--<property name="defaultEncoding" value="ISO-8859-1"/>-->
</bean>
<mvc:interceptors>
<!-- 该拦截器通过名为”locale”的参数来拦截HTTP请求,使其重新设置页面的区域化信息 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 设置请求的参数名为locale -->
<property name="paramName" value="locale"/>
</bean>
</mvc:interceptors>
<!-- SessionLocaleResolver解析器 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<!-- 设置session attribute的key -->
<property name="localeAttributeName" value="locale"/>
<!-- 设置默认的Locale -->
<property name="defaultLocale" value="en"/>
</bean>
- 属性文件
参考https://segmentfault.com/a/1190000014797899中的项目结构,本章与其一致。
- 控制器
编写Controller控制器,以便测试,代码如下:
@GetMapping(value = "/getSessionLocale", produces = "text/html;charset=UTF-8")
@ResponseBody
public String sessionLocaleResolver(HttpServletRequest request) {
RequestContext requestContext = new RequestContext(request);
String value = requestContext.getMessage("message.locale");
HttpSession session = request.getSession();
return "Session中设置的Locale是:"+session.getAttribute("locale")+" </br>当前使用的Locale是:" + requestContext.getLocale() + " </br>使用的资源Locale文件是:messages_" + value+".properties";
}
- 测试
浏览器发起请求http://localhost:8089/getSessionLocale?locale=en_US,结果如下图:
变更参数locale的值,请求http://localhost:8089/getSessionLocale?locale=zh_CN,结果如下图:
测试结果表明Locale设置成功,本例验证了SessionLocaleResolver的使用。
总结
- 使用SessionLocaleResolver与LocaleChangeInterceptor结合使用来设置国际化
- 大致流程为:根据请求的语言参数,在过滤器中设置Locale,Spring就可以根据设置区不同的属性文件来实现国际化
- 本系列主要分析了Spring MVC国际化的原理,有写的不到位的地方还望好好包涵,有不书面不清晰的地方可留言,真心地希望跟大家一起交流探讨。
最后创建了qq群方便大家交流,可扫描加入,同时也可加我qq:276420284,共同学习、共同进步,谢谢!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。