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. Dev BarManager使用方法

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking 近期使用BarManager时候.发现一个问题就是在一開始把BarManager控件拖到窗口上的时 ...

  2. 摘记—Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha

    What a Unicode string ? The binaries in RAM have the final word. NOT string literals in Text Editor( ...

  3. Amoeba软件实现mysql读写分离

    一般不用,大公司都是自己程序实现的. 安装amoeba

  4. c# 获取当前程序运行根目录

    //获取绝对路径,调用如 string fileName = string.Format("~/RuleConfigFiles/Campaign_{0}.JSON", Campai ...

  5. cocos2d-x JsonBox 读写

    #include "JsonBox.h" std::string path = "test.json”; //注意引入路径 path = cocos2d::CCFileU ...

  6. 每日英语:China Overtakes U.S. in Number of Diabetes Cases

    China is now home to the world's largest diabetes population. The number of people who have diabetes ...

  7. destoon的如何显示tag生成的sql语句

    destoon 如何显示tag生成的sql语句 在tag.func.php中第117行加入 echo $query;  就只可以了

  8. js 学习的地址;

    1.https://github.com/windiest/Front-end-tutorial      2.http://www.w3school.com.cn/tags/html_ref_eve ...

  9. C#中http请求下载的常用方式demo

    //通过webClient方式 WebClient client = new WebClient(); string url="http://down6.3987.com:801/2010/ ...

  10. Jquery 禁用元素的所有属性

    jQuery有一个.prop()的方法,可以让你来调整元素的属性. 以下代码显示如何禁用所有按钮: $("button").prop("disabled", t ...