SpringMVC配置国际化出现的异常

       使用SpringMVC3配置国际化,最近遇到了“Cannot change HTTP accept header - use a different locale resolution strategy” 这样的异常提示,最终解决了。现在来说说有关SpringMVC配置国际化的步骤及注意点,最后结合spring源码解析下原理。

       国际化最常需要解决的问题

  1.       页面上能够根据浏览器设置的语言来动态展现文本,数值,时间等;
  2.       可以在Bean中获取设置的Locale信息;
  3.       可以通过动态链接的方式来切换locale信息。

        以下以demo的方式来说明以上三个问题的常用解决方法。

        问题1,解决方式如下:

        配置ResourceBundleMessageSource解析器,配置springmvc.xml如下:

       

  1. <!– 国际化 –>  
  2.     <bean id=“messageSource”  
  3.         class=“org.springframework.context.support.ResourceBundleMessageSource”>  
  4.         <property name=“basename” value=“i18n”></property>  
  5.     </bean>  
<!-- 国际化 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>

        增加i18n.properties配置文件,demo像征性的配置了一个中文,一个英文的,如下图:

     

            i18n_zh_CN.properties内容:

  1. i18n.username = \u7528\u6237\u540D  
  2. i18n.password = \u5BC6\u7801  
i18n.username = \u7528\u6237\u540D
i18n.password = \u5BC6\u7801
          i18n_en_US.properties内容:
  1. i18n.username = username  
  2. i18n.password = passsword  
i18n.username = username
i18n.password = passsword

           页面展现采用了JSTL的fmt标签:

  1. <fmt:message key=“i18n.username”></fmt:message>  
<fmt:message key="i18n.username"></fmt:message>

有以上配置即可实现根据浏览器设置来展现 “用户名” 或是英文的 “username” 。


               问题2,在Java中在Bean中获取locale设置信息

           这个比较简单,在Bean中注入ResourceBundleMessageSource,即可获取到了,相关Java代码如下:


  1. @Autowired  
  2.     private ResourceBundleMessageSource messageSource;  
  3.       
  4.     /**  
  5.      * 1.验证国际化配置根据本地语言配置动态,利用fmt标签动态展现  
  6.      * 2.验证在Bean中获取国际化资源文件Locale对应的信息  
  7.      */  
  8.     @RequestMapping(“/i18n”)  
  9.     public String testI18n(Locale locale) {  
  10.         String user = messageSource.getMessage(“i18n.username”, null, locale);  
  11.         System.out.println(“国际化资源文件Locale配置(username):”+user);  
  12.         return “i18n”;  
  13.     }  
@Autowired
    private ResourceBundleMessageSource messageSource;

    /**
     * 1.验证国际化配置根据本地语言配置动态,利用fmt标签动态展现
     * 2.验证在Bean中获取国际化资源文件Locale对应的信息
     */
    @RequestMapping("/i18n")
    public String testI18n(Locale locale) {
        String user = messageSource.getMessage("i18n.username", null, locale);
        System.out.println("国际化资源文件Locale配置(username):"+user);
        return "i18n";
    }

              问题3,再简单描述下就是在页面上动态设置语言信息,即动态设置locale信息以达到切换展现的目的,如下图:


在实现这个问题时遇到一个了标题中所写的报错,究其原因单步源码发现是由于localeResovler默认注入的是org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver,我们查看下这个类的setLocale方法会发现出现异常的原因了。

  1. public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {  
  2. /* 45 */     throw new UnsupportedOperationException(“Cannot change HTTP accept header - use a different locale resolution strategy”);  
  3. /*    */   }  
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
/* 45 */     throw new UnsupportedOperationException("Cannot change HTTP accept header - use a different locale resolution strategy");
/*    */   }

那么我们可以注入org.springframework.web.servlet.i18n.SessionLocaleResolver这个解析器来解决,这个解析器主要是将获取后Locale对象存于Session中供后续获取。

springmvc.xml配置如下:

  1. <!– 配置SessionLocaleResolver用于将Locale对象存储于Session中供后续使用 –>  
  2.     <bean id=“localeResolver”  
  3.         class=“org.springframework.web.servlet.i18n.SessionLocaleResolver”></bean>  
  4.   
  5. <!– 配置LocaleChangeInterceptor 主要用于获取请求中的locale信息,将期转为Locale对像,获取LocaleResolver对象–>  
  6.     <mvc:interceptors>  
  7.         <bean class=“org.springframework.web.servlet.i18n.LocaleChangeInterceptor”></bean>  
  8.     </mvc:interceptors>  
<!-- 配置SessionLocaleResolver用于将Locale对象存储于Session中供后续使用 -->
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

<!-- 配置LocaleChangeInterceptor 主要用于获取请求中的locale信息,将期转为Locale对像,获取LocaleResolver对象-->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>

这里有个特别需要注意bean id的配置,值必须为localeResolver否则,仍然会报标题中的错误。

按以上操作之后,就可以不用去浏览器手工设置语言了,可以直接通过上图中的 “中文”,“英文”进行切换,相信有些朋友会遇到这样的需求。

结合单步源码的过程,这里理解的内部原理用下图表示下:


       


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值