码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC国际化

时间:2014-11-19 17:51:33      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   sp   java   strong   文件   on   

本文基于Spring MVC 注解-让Spring跑起来。本文提到的国际化是Spring实现国际化的方案之一。

        (1) 在applicationContext.xml中添加以下配置信息:

 

[java] view plaincopy
 
  1.        <!-- 国际化配置 -->  
  2. <bean id="messageSource"  
  3.     class="org.springframework.context.support.ResourceBundleMessageSource">  
  4.     <property name="basename" value="messages.messages" />  
  5. </bean>  

        上述代码中提到的messages.messages中,前一个messages是src下的一个文件夹,后一个messages是所有以messages开头的,以properties结尾的文件,如messages_zh_CN.properties或messages_en.properties,这些文件即是配置国际化信息的文件,其信息分别如下:

 

 

[java] view plaincopy
 
  1. #message_en.properties  
  2. main.title=RUI manage system  

 

[java] view plaincopy
 
  1. #message_zh_CN.properties  
  2. main.title=RUI管理系统  



        (2) 在dispatcher.xml中添加以下配置信息

 

 

[java] view plaincopy
 
  1. <!-- 国际化配置 -->  
  2. <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />  


        并添加拦截器配置:

 

 

[java] view plaincopy
 
  1. <mvc:interceptors>  
  2.     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  
  3. </mvc:interceptors>  

 

        (3) 在jsp页面中调用国际化后的信息

 

[java] view plaincopy
 
  1. <fmt:message key="main.title" />  



 

        (4) 访问系统,只需要在初次访问系统时,在地址拦上添加"?locale=en"即可访问英文网站。注意,访问系统的第一个页面时添加即可,后绪可不再添加。

Spring MVC国际化

标签:blog   http   io   ar   sp   java   strong   文件   on   

原文地址:http://www.cnblogs.com/toSeeMyDream/p/4108335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!