Spring MVC国际化

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

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

[java]  view plain copy
  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 plain copy
  1. #message_en.properties  
  2. main.title=RUI manage system  

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


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

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

        并添加拦截器配置:

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

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

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


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



 本文基于Spring MVC国际化

        在Spring MVC国际化一文中描述了如何实现Spring的国际化,也描述了在jsp页面中如何获取国际化信息,本文描述如何在java代码中获取国际化信息。

        在Java代码中,获取国际化信息使用org.springframework.web.context.WebApplicationContext的getMessage方法,getMessage方法中需要使用当前的Locale信息,于是,怎样获取国际化信息集中在以下两点:

        1. 如何获取WebApplicationContext?

        2. 如何获取当前使用的Locale?

        首先,如何获取WebApplicationContext呢,很简单,在任何一个类实现org.springframework.web.context.ServletContextAware并重写其setServletContext方法,如下所示:

[java]  view plain copy
  1. public class MenuServiceImpl implements MenuService, ServletContextAware {  
  2.         private ServletContext servletContext;  
  3.   
  4.     /* 
  5.      * (non-Javadoc) 
  6.      *  
  7.      * @see 
  8.      * org.springframework.web.context.ServletContextAware#setServletContext 
  9.      * (javax.servlet.ServletContext) 
  10.      */  
  11.     @Override  
  12.     public void setServletContext(ServletContext servletContext) {  
  13.         this.servletContext = servletContext;  
  14.     }  
  15.   
  16.     public void test() {  
  17.         WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);  
  18.     }  
  19. }  

        如此,我们取得了WebApplicationContext信息。

        其次,如何获取当前使用的Locale信息?一行代码搞定:

[java]  view plain copy
  1. Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
        WebApplicationContext信息和Locale信息都获取到后,再用一行代码获取国际化信息:

[java]  view plain copy
  1. String menuName = applicationContext.getMessage("text.menu.name",null"菜单A", locale);  
        在getMessage中有四个变量,依次分别为message_*.properties文件中的key,key中{0}、{1}等对应的值,默认值和Locale。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值