开发者社区> 问答> 正文

springmvc国际化问题 java报错

我配置了国际化

<!-- 国际化支持 -->
	<mvc:interceptors>
		<ref bean="localeChangeInterceptor" />
	</mvc:interceptors>
	
	<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
		<property name="paramName" value="locale" />
	</bean>
	<bean id="localeResolver"  class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>  
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename">
			<value>messages</value>
		</property>
	</bean>


jsp部分代码:

<spring:message code="login.title"/>

访问方式1:

http://localhost:8080/testi18n/hello.do(访问直接返回jsp)

访问方式2:

http://localhost:8080/testi18n/index.jsp


方式1可以正常,但是方式2就报异常了

javax.servlet.jsp.JspTagException: No message found under code 'login.title' for locale 'zh'.


这是为什么呢?



展开
收起
huc_逆天 2020-05-27 21:27:47 746 0
1 条回答
写回答
取消 提交回答
  • 国际化最常需要解决的问题

      页面上能够根据浏览器设置的语言来动态展现文本,数值,时间等;
      可以在Bean中获取设置的Locale信息;
      可以通过动态链接的方式来切换locale信息。
        以下以demo的方式来说明以上三个问题的常用解决方法。
        问题1,解决方式如下:
    
        配置ResourceBundleMessageSource解析器,配置springmvc.xml如下:
    

    [html] view plain copy

    <!– 国际化 –>


    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>
    

    1 2 3 4

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

    [html] view plain copy

    i18n.username = \u7528\u6237\u540D
    i18n.password = \u5BC6\u7801
    i18n.username = \u7528\u6237\u540D i18n.password = \u5BC6\u7801 1 i18n_en_US.properties内容: [html] view plain copy

    i18n.username = username
    i18n.password = passsword
    i18n.username = username i18n.password = passsword 1 页面展现采用了JSTL的fmt标签:

    [html] view plain copy

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

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

               问题2,在Java中在Bean中获取locale设置信息
    
           这个比较简单,在Bean中注入ResourceBundleMessageSource,即可获取到了,相关Java代码如下:
    

    [html] view plain copy

    @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”;  
    }  
    

    @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";
    }
    

    1 2 3 4 5 6 7 8 9 10 11 12

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

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

    [html] view plain copy

    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    /* 45 / throw new UnsupportedOperationException(“Cannot change HTTP accept header - use a different locale resolution strategy”);
    /
    / }
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { /
    45 / throw new UnsupportedOperationException("Cannot change HTTP accept header - use a different locale resolution strategy"); / */ } 1 2 那么我们可以注入org.springframework.web.servlet.i18n.SessionLocaleResolver这个解析器来解决,这个解析器主要是将获取后Locale对象存于Session中供后续获取。

    springmvc.xml配置如下:

    [html] view plain copy

    <!– 配置SessionLocaleResolver用于将Locale对象存储于Session中供后续使用 –>

    <!– 配置LocaleChangeInterceptor 主要用于获取请求中的locale信息,将期转为Locale对像,获取LocaleResolver对象–>
    mvc:interceptors

    </mvc:interceptors>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
    
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>
    

    1 2 3 4 5 6 7 8

    这里有个特别需要注意bean id的配置,值必须为localeResolver否则,仍然会报标题中的错误。 按以上操作之后,就可以不用去浏览器手工设置语言了,可以直接通过上图中的 “中文”,“英文”进行切换,相信有些朋友会遇到这样的需求。

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

    2021-02-21 01:48:53
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Java 8 简明教程 立即下载
Java高级特性入门(二) 立即下载
Java Your(Next) 立即下载