先说说异常的原因!!!
着实令人头痛!不论是.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"/>