在我的应用程序中,我已经定义了以下 bean,但是当我尝试使用参数 ex 更改区域设置时:?lang=es 它对我不起作用并且显示的是 CookieLocaleResolver 中定义的默认区域设置,即英语。
这是我的 bean 类:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="resources/i18n/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="lang" />
<property name="defaultLocale" value="en" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
我想要实现的是使用参数更改语言并将其保存在与之相关的 cookie 中,以便进一步请求语言环境是新的语言环境!
我也不想使用下面的类,因为它已经被弃用了:
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
我的声明式 bean 实现有什么问题?
最佳答案
我相信你应该这样定义interceptor
:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="resources/i18n/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="lang" />
<property name="defaultLocale" value="en" />
</bean>
MVC 命名空间定义如下:
xmlns:mvc="http://www.springframework.org/schema/mvc"
....
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
确保使用正确的 xsd 版本。
关于java - Spring MVC 区域设置更改不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389154/