为了让web应用程序支持国际化,必须识别每一个用户的首选区域,并根据这个区域显示内容。web
在Spring MVC应用程序中,用户的区域是经过区域解析器来识别的,它必须实现LocaleResolver接口。Spring MVC提供了几个LocaleResolver实现,让你能够按照不一样的条件来解析区域。除此以外,你还能够实现这个接口,建立本身的区域解析器。spring
要定义一个区域解析器,只需在web应用程序上下文中注册一个LocaleResolver类型的Bean就能够了。你必须将区域解析器的Bean名称设置为localeResolver,这样DispatcherServlet才能自动侦测到它。请注意,每DispatcherServlet只能注册一个区域解析器。浏览器
1.按HTTP请求头部解析区域cookie
Spring采用的默认区域解析器是AcceptHeaderLocaleResolver。它经过检验HTTP请求的accept-language头部来解析区域。这个头部是由用户的web浏览器根据底层操做系统的区域设置进行设定。请注意,这个区域解析器没法改变用户的区域,由于它没法修改用户操做系统的区域设置。mvc
2.按会话属性解析区域app
解析区域的另外一种方法是经过SessionLocaleResolver。它经过检验用户会话中预置的属性来解析区域。若是该会话属性ide
不存在,它会根据accept-language HTTP头部肯定默认区域。ui
Xml代码
<bean id="localeResolver" class="org.springframewrok.web.servlet
.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
若是会话属性不存在,能够为这个解析器设置defaultLocale属性。请注意,经过修改保存该区域的会话属性,这个区域this
解析器能够改变用户的区域设置。spa
3.按Cookie解析区域
你也能够检验用户浏览器中的Cookie,用CookieLocaleResolver来解析区域。若是Cookie不存在,它会根据accept-language HTTP头部肯定默认区域。
Xml代码
<bean id="localeResolver" class="org.springframework.web.servlet
.i18n.CookieLocaleResolver"/>
这个区域解析器所采用的Cookie能够经过cookieName和cookieMaxAge属性进行定制。cookieMaxAge属性表示这个Cookie应该持续多少秒,-1表示这个Cookie在浏览器关闭以后就失效。
Xml代码
<bean id="localeResolver" class="org.springframework.web.servlet
.i18n.CookieLocaleResolver">
<property name="cookieName" value="language"/>
<property name="cookieMaxAge" value="3600"/>
<property name="defaultLocale" value="en"/>
</bean>
若是用户浏览器中不存在该Cookie,你也能够为这个解析器设置defaultLocale属性。经过修改保存该区域的Cookie,这个区域解析器可以改变用户的区域。
4.FixedLocaleResolver
一直使用固定的Local, 改变Local 是不支持的 。
5.修改用户的区域
除了显式调用LocaleResolver.setLocale()来修改用户的区域以外,还能够将LocaleChangeInterceptor拦截器应用处处理程序映射中,它会发现当前HTTP请求中出现的特殊参数。其中的参数名称能够经过拦截器的paramName属性进行自定义。若是这种参数出如今当前请求中,拦截器就会根据参数值来改变用户的区域。
Xml代码
<beans...>
...
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language"/>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
...
<ref bean="localeChangeInterceptor"/>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.support
.ControllerClassNameHandlerMapping">
<property name="interceptors">
<list>
...
<ref bean="localeChangeInterceptor"/>
</list>
</property>
</bean>
<beans>
LocaleChangeInterceptor只能为启用了它的那些处理程序映射侦测参数。所以,若是web应用程序上下文中配置了不止一个处理程序映射,就必须在全部处理程序映射中注册这个拦截器,以便能在任意URL中改变它们的区域设置。
如今,利用language参数,能够经过任意URL修改用户的区域。例如,下面两个URL分别将用户的区域语言改为了美式
英语和德语。
Java代码
http://localhost:8080/court/welcome.htm?language=en_US
http://localhost:8080/court/welcome.htm?language=de
6.使用Spring MVC时, controller如何获得请求的 Local
DispatchServlet 会在 processRequest(HttpServletRequest request, HttpServletResponse response) 方法中设置LocaleContext, 把LocalContext 和当前线程关联起来. 代码以下:
LocaleContextHolder.setLocaleContext (buildLocaleContext(request), this. threadContextInheritable );
DispatchServlet 中buildLocalContext代码以下:
protected LocaleContext buildLocaleContext( final HttpServletRequest request) {
return new LocaleContext() {
public Locale getLocale() {
return localeResolver .resolveLocale(request);
}
@Override
public String toString() {
return getLocale().toString();
}
};
}
这里的Local经过localResolver 解析获得, localResolver 便是从Spring 配置文件配置的localResolver, 默认是"AcceptHeaderLocaleResolver".
若是你想要在 controller 中获得当前请求的Local, 代码能够以下写:
Locale locale = LocaleContextHolder.getLocale();
或者你能够用Spring 中的RequestContextUtils 类方法getLocal获得 request 中保存的localResolver, 并用localResolver 解析获得Local. 代码以下:
public static Locale getLocale (HttpServletRequest request) {
LocaleResolver localeResolver = getLocaleResolver (request);
if (localeResolver != null ) {
return localeResolver.resolveLocale(request);
}
else {
return request.getLocale();
}
}
localResolver 会在DispatcherServlet的doService 方法中,将localResolver保存到request 属性中 代码以下:
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
---------------------
做者:rj042
来源:CSDN
原文:https://blog.csdn.net/rj042/article/details/23354225
版权声明:本文为博主原创文章,转载请附上博文连接!
@ https://blog.csdn.net/qq924862077/article/details/52878507