Spring 的MVC I18N-国际化相关配置
2008-06-05 15:54
465 查看
Spring 的MVC I18N-国际化相关配置 收藏
function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}
一、基于浏览器语言的国际化配置
使用Spring的MVC,并且配置中有配置Resource文件
<!-- 资源文件绑定器 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="message-info" /> <property name="useCodeAsDefaultMessage" value="true" /> </bean> |
只要有了这个配置,然后配置JSP渲染器为JSTL支持的,那么在你的JSP文件中使用fmt标记就可以实现客户浏览器语言国际化了。
如:<fmt:message key="info.login.title" />
其中的info.login.title和你的资源文件对应
二、基于动态加载的国际化配置
1、基于请求的国际化配置
基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主。
配置方式如下:
首先配置拦截器
<!-- 国际化操作 拦截器 必需配置,可以和其它国际化方式通用 --> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> |
配好上面的拦截器之后,就将拦截器注入到你的UrlHandlerMapping中,例如:
<bean id="defaultUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="interceptors" ref="localeChangeInterceptor" /> <property name="order"> <value>1</value> </property> </bean> |
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"> </bean> |
<a href="?locale=zh_CN">简体中文</a> |
2、基于Session的国际化配置
拦截器和基于请求的相同
Session的配置如下:
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> </bean> |
SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME |
3、基于Cookie的国际化配置
这个我就不说了,反正用的不多,至少我做的项目能不用Cookie就不用Cookie,所以,基于Cookie的国际化配置我就不细说了,如果想知道怎么配置,那么下载一个Spring,其中的例子程序就是用Cookie配置的,你自己读代码就OK了。
三、注意事项
如果不用默认的浏览器语言国际化方式,那么拦截器一定要配置,如果你有多个UrlMapping,那么就每个都配上拦截器。
至于配置的LocaleResolver的名字,一定要用上面的配置中的名字localeResolver当然了,这个是默认的名字来的,自己设置成别的也可以,但是就是麻烦,反正我用默认的就感觉不错
相关文章推荐
- Spring 的MVC I18N-国际化相关配置出现的问题
- Spring 的MVC I18N-国际化相关配置
- 关于转载Spring 的MVC I18N-国际化相关配置出现的问题
- Spring 的MVC I18N-国际化相关配置
- spring mvc @ResponseBody/@RequestBody 配值action直接把对象输入json格式输入到前端相关配置
- spring 国际化i18n配置
- Spring MVC+Freemarker+Javascript的多语言(国际化i18n/本地化)和主题(Theme)实现
- spring中配置<aop:aspectj-autoproxy/>与<mvc:annotation-driven/>作用以及国际化配置
- spring-mvc开发模式相关配置
- Intellij IDEA使用(十二)—— 使用Intellij IDEA导入Eclipse创建的Spring+SpringMVC+Mybatis(SSM)整合项目并修改相关配置
- Springmvc+Redis使用jedisTemplete来操作redis的相关配置和代码示例
- SpringMVC Interceptor 配置由简单到复杂及相关异常解决办法,记录用户操作日志
- Spring MVC+Freemarker+Javascript的多语言(国际化i18n/本地化)和主题(Theme)实现
- springMVC Controller 和 Service 通过@value 读取properties配置信息
- 将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
- spring mvc+mybatis+sql server简单配置
- Eclipse+maven配置SSM(Spring+SpringMVC+mybatis)
- Spring+SpringMVC+Mybatis配置(Maven项目)
- spring+mybatis+springmvc项目配置
- Jeesite增加spring-data-redis缓存功能以及相关配置