http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-localeresolver

Spring架构的多数部分都支持国际化,如同Spring Web MVC这样。DispatcherServlet使得你可以自动的处理messages -- 使用客户端的locale。 这是通过LocaleResolver对象完成的。

当一个请求进来时,DispatcherServlet会查找一个locale resolver,如果找到了一个,它会试着使用它来设置locale。 使用RequestContext.getLocale()方法,你可以一直获取到由locale resolver处理过的locale。

除了自动的locale处理,你还可以添加一个拦截器来处理映射,以在特定情景下改变locale,例如,基于请求中的某个parameter。

Locale resolvers和拦截器,都是定义在包 org.springframework.web.servlet.i18n 中,然后在你的应用context中以常规方式配置。 这里是Spring中locale resolvers的一个选择。

1、获取Time Zone信息

除了获取客户端的locale,还经常需要知道他们的时区信息。LocaleContextResolver接口扩展了LocalResolver,允许resolvers提供更加丰富的LocaleContext -- 可能含有时区信息。

当可以的时候,用户的TimeZone 可以使用RequestContext.getTimeZone()获取。 Spring ConversionService中注册的Date/Time Converter和Formatter会自动的使用时区信息。

2、AcceptHeaderLocaleResolver

这个locale resolver会检查请求中的accept-language header。一般,该header字段包含了客户端操作系统的locale。

注意,该locale resolver不支持时区信息。

3、CookieLocaleResolver

该locale resolver会检查可能存在于客户端的Cookie,以判断是否指定了Locale或者TimeZone。如果有,就会使用指定的信息。通过使用该locale resolver的properties,你可以指定cookie的名字以及寿命。 下例:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">

    <property name="cookieName" value="clientlanguage"/>

    <!-- in seconds. If set to -1, the cookie is not persisted (deleted when browser shuts down) -->
<property name="cookieMaxAge" value="100000"/> </bean>

Table 22.4. CookieLocaleResolver properties

Property Default Description

cookieName

classname + LOCALE

The name of the cookie

cookieMaxAge

Integer.MAX_INT

The maximum time a cookie will stay persistent on the client. If -1 is specified, the cookie will not be persisted; it will only be available until the client shuts down their browser.

cookiePath

/

Limits the visibility of the cookie to a certain part of your site. When cookiePath is specified, the cookie will only be visible to that path and the paths below it.

4、SessionLocaleResolver

SessionLocaleResolver 允许你从session中获取Locale和TimeZone。 相对于CookieLocaleResolver,这种策略会在Servlet容器的HttpSession中存储选中的locale设置。相应的,这些设置相对每个session来说都是临时的,并且,当每个session结束时会丢失。

注意,这和外部session管理机制没有直接的关系 -- 如Spring Session project。这个SessionLocaleResolver会简单的评估和修改当前HttpServletRequest相应的HttpSession attributes。

5、LocaleChangeInterceptor

通过将LocaleChangeInterceptor添加到handler mappings之一,你可以启用locales的改变。它会探测request中的一个parameter,并修改locale。 它会调用LocaleResolver的setLocale()。下例示意了调用含有名字为siteLanguage的parameter的所有”*.view”资源,会立即改变locale。对该例来说,请求”http://www.sf.net/home.view?siteLanguage=nl” 会改变site语言为Dutch。

<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage"/>
</bean> <bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> <bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>/**/*.view=someController</value>
</property>
</bean>

Spring 4 官方文档学习(十一)Web MVC 框架之locales的更多相关文章

  1. Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC

    内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...

  2. Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图

    接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolv ...

  3. Spring 4 官方文档学习(十一)Web MVC 框架

    介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...

  4. Spring 4 官方文档学习(十二)View技术

    关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...

  5. Spring Boot 官方文档学习(一)入门及使用

    个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...

  6. Spring boot官方文档学习(一)

    个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...

  7. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)

    接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...

  8. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

  9. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)

    题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...

  10. Spring 4 官方文档学习(十一)Web MVC 框架之编码式Servlet容器初始化

    在Servlet 3.0+ 环境中,你可以编码式配置Servlet容器,用来代替或者结合 web.xml文件.下面是注册DispatcherServlet : import org.springfra ...

随机推荐

  1. List 集合remove问题

    java的list集合中.使用remove删除元素: 方法一: static List<Integer> list3 = new ArrayList<Integer>(); s ...

  2. Note for video Machine Learning and Data Mining——training vs Testing

    Here is the note for lecture five. There will be several points  1. Training and Testing  Both of th ...

  3. 编码规范:Eclipse Checkstyle配置

    http://chenzhou123520.iteye.com/blog/1627618 http://www.cnblogs.com/lanxuezaipiao/p/3202169.html

  4. JavaScript与DOM(下)

    介绍 上一章我们介绍了JavaScript的基本内容和DOM对象的各个方面,包括如何访问node节点.本章我们将讲解如何通过DOM操作元素并且讨论浏览器事件模型. 本文参考:http://net.tu ...

  5. hadoop(2.x)以hadoop2.2为例完全分布式最新高可靠安装文档

    问题导读:1.如何配置各个节点之间无密码互通?2.启动hadoop,看不到进程的原因是什么?3.配置hadoop的步骤是什么? 4.有哪些配置文件需要修改?5.如果没有配置文件,该如何找到该配置文件? ...

  6. WCF使用net.tcp绑定时的注意事项

    IIS Express没有net.tcp绑定功能,本地测试的话只能使用本机的IIS进行承载,并且需要相应的配置(参见上一篇文章). 算了,直接举一个配置例子吧,懒得写了... <system.s ...

  7. winphone开发环境配置

    环境:操作系统win7 要进行winphone开发,必须进行一些环境的配置.下面是我的一些配置总结. 1.操作系统 winphone开发仅仅能在win8下开发.所以首先得安装win8.能够使用nt6 ...

  8. JDK自带工具之问题排查场景示例

    最近看到了大量关于java性能调优.故障排查的文章,自己也写了一篇< Java调优经验谈 >.接着此篇文章,其实一直打算写写一些常用调优工具以及它们的惯常用法的.后来在http://jav ...

  9. javascript高级:原型与继承

    原型继承的本质就是一条原型链,对象会沿着这条链,访问链里的方法属性. 对象的__proto__属性就是用于访问它的原型链的上一层: 考虑以下对象: 1. 所有对象的原型: Object.prototy ...

  10. vue轮播图插件vue-awesome-swiper的使用与组件化

    不管是APP还是移动端网页开发,轮播图在大部分项目当中都是存在的,这时候如果用vue开发项目,选择一款好的插件并且封装好是很重要的 1. 推荐使用vue-awesome-swiper 安装:cnpm ...