springMVC 三种国际化配置方式及No message found under code 'language.cn' for locale 'zh_CN'.异常
2018-03-09 15:55
781 查看
先说说异常的原因!!!
着实令人头痛!不论是.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>
4000
<!-- 国际化操作拦截器如果采用基于(Session/Cookie则必须配置) -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
着实令人头痛!不论是.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>
4000
<!-- 国际化操作拦截器如果采用基于(Session/Cookie则必须配置) -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
相关文章推荐
- 关于javax.servlet.jsp.JspTagException: No message found under code 'language.cn' for locale 'zh_CN'.
- NO message found under code 'xxxxx' locale 'zh_CN'
- No message found under code 'login.error' for locale 'zh'. 的解决办法
- springmvc 国际化i18n错误: No message found under code解决方法
- No message found under code 'login.userName' for locale 'en_GB' error in spring MVC
- 关于javax.servlet.jsp.JspTagException: No message found under code 'name' for locale 'zh_CN'.错误的原因分析
- IOError: No translation files found for default language zh-cn.
- IOError: No translation files found for default language zh-cn.
- No message found under code 'xxx' for locale 'zh_CN'.
- IOError: No translation files found for default language zh-cn.
- Missing message for key "err1" in bundle "(default bundle)" for locale zh_CN
- Cannot open the message catalog "man" for locale "zh_CN.UTF-8"
- SpringMVC中静态资源无法找到(No mapping found for HTTP request with URI)的四种解决方式
- Cannot open the message catalog "man" for locale "zh_CN.UTF-8"
- SpringMVC restful 异常Could not write request: no suitable HttpMessageConverter found for request type
- JSP页面错误:No form found under in locale 'zh_CN'
- 关于Spring 国际化 No message found under code 的解决方案
- springMVC 使用ajax 出现No serializer found for class异常
- springMVC 使用ajax 出现No serializer found for class异常
- HttpMessageConverter配置javaconfig(未配置异常:No converter found for return value of type)