springMVC 三种国际化配置方式及No message found under code 'language.cn' for locale 'zh_CN'.异常

先说说异常的原因!!!

着实令人头痛!不论是.xml配置文件还是.properties配置文件都检查了几次,而且都没有问题。偶然间在资料上注意到三种国际化配置,其请求都经过中央调度器DispatcherServlet,经url映射到controller处理后,将view返回给用户,视图中才能正确反映!! 问题就出在了这里,我直接访问静态资源!!!导致资源文件没有加载、也不经过拦截器。


jsp页面使用message标签就可以获取国际化资源文件信息:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<spring:message code="mainpage"/>

国际化配置:

一、AccepetHeaderLocaleResolver:基于浏览器请求的国际化,springMVC从浏览器中读取accept-language语言区域,从而就可以确定了具体语言:

spring-mvc.xml中配置:

    <!-- 加载国际化资源文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="messages"/>
    </bean>
    <mvc:interceptors>
       <!-- 国际化操作拦截器如果采用基于(Session/Cookie则必须配置) -->
       <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

jsp中:

<body> 
	<a href="frame/main.jsp"><spring:message code="mainpage"/>
	</a>>>>
     <spring:message code="votemanage"/>

二、SessionLocaleResolver国际化:spring mvc 会从HttpSession作用域中获取用户所设置的语言区域,所以经controller处理需要将具体语言设置在session范围中,经拦截器处理后则可正确显示:

    <!-- 加载国际化资源文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="messages"/>
    </bean>
    <mvc:interceptors>
       <!-- 国际化操作拦截器如果采用基于(Session/Cookie则必须配置) -->
       <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

三、CookieLocaleResolver:spring mvc会从Cookie中获取用户设置的语言区域,因此经controller处理时需将具体语言设置在Cookie中:

    <!-- 加载国际化资源文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="messages"/>
    </bean>
    <mvc:interceptors>
       <!-- 国际化操作拦截器如果采用基于(Session/Cookie则必须配置) -->
       <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值