Spring对国际化的支持
来源:互联网 发布:mysql查询count 编辑:程序博客网 时间:2024/04/29 19:39
Spring使用拦截器支持国际化很方便,使用时只需要两个步骤:
一.spring配置
具体配置方式如下:
<!-- 资源文件绑定器,文件名称:messages.properties(没有找到时的默认文件), messages_en.properties(英文),messages_zh_CN.properties(中午),等等-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="config.messages.messages" />
</bean>
<!-- 定义本地化变更拦截器 -->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<util:list id="interceptors">
<ref bean="localeChangeInterceptor" />
</util:list>
<!-- 定义注解URL映射处理器 ,所有的请求映射必须关联本地化拦截器-->
<bean id="urlMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors" ref="interceptors" />
<property name="order" value="1"></property>
</bean>
这时还需要本地化处理器进行处理,有三种处理器,如下:
<!-- 定义本地化处理器 -->
1. 基于session
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
2. 基于请求
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>
3.基于cookie
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
以上三种处理器理论上配置任意一个就可以,不过有两点需要注意:
1)第二个不能直接使用,需要通过继承重写相应的方法,如下:
public class MyLocaleResolver extends AcceptHeaderLocaleResolver{
private Locale myLocal;
public Locale resolveLocale(HttpServletRequest request) {
return myLocal==null?request.getLocale():myLocal;
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
myLocal = locale;
}
}
修改配置文件中的localeResolver配置,class指向这个类就可以了。
2)第一与第三个用法相同,只不过前者使用session,session过期就需要重新设置,而后者使用cookie,可以根据项目的具体情况进行选择。
一般来说,使用第一种和第三种比较常见。
二.使用方法
当不做处理时默认会使用浏览器自己的语言设置,如果想改变语言,只需要在请求后面加上一个参数即可,
默认的参数名为locale,里面放的就是你的提交参数,如:en_US,zh_CN之类的,
所以,只需要在页面上加上相应的链接即可,如下:
<a href="xxx.do?locale=zh_CN">中文</a>
<a href="xxx.do?locale=en">英文</a>
页面中使用jstl或spring标签,如下:
<fmt:message key="test.app"/>
<s:message code="test.app"/>
无论使用哪种处理器,locale设置过一次就可以了,不需要每个连接后面都加上locale参数。
- Spring对国际化的支持
- Struts2对国际化的支持
- Struts对国际化的支持
- struts2对国际化的支持
- Struts对国际化的支持
- JDK对国际化的支持
- Spring Applicationcontext的国际化支持
- Spring Applicationcontext的国际化支持
- Spring ApplicationContext的国际化支持
- Spring 国际化的支持:Spring MVC Velocity
- JAVA语言对国际化支持的原理
- iOS 对国际化语言的支持.
- iOS 对国际化语言的支持.
- Spring -- 国际化的支持(MessageResource用法)
- Spring 国际化支持
- spring 国际化支持
- spring国际化支持
- Spring支持国际化
- .net自带wsdl.exe的用法与参数说明
- 脏污图片
- xp自动登录
- 利用fdisk进行磁盘分区
- 鼠标指针改变
- Spring对国际化的支持
- MFC中定义全局变量的方法
- ONVIF标准
- 软件质量----题
- SQL消除空格列
- 配置oem
- PhysX官方教程lession101 [山寨翻译版]
- 无刷新,动态设置页面焦点的问题
- 编程牛博