在Spring MVC中更改过滤器类中的语言环境

时间:2013-12-27 03:52:36

标签: java spring spring-mvc

如何在springMVC中更改过滤器类中的语言环境。

在我的过滤器类中,我正在使用下面的代码:

LocaleContextHolder.setLocale(new Locale(lang));

我传递 lang 值是“ms”

当我从UI更改语言环境时,它运行正常。我想要改变过滤器calss,它不起作用。

在我配置的 spring-servlet.xml 下面。

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />    
</bean>
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName">
            <value>locale</value>
        </property>
    </bean>
</mvc:interceptors>

Web.xml中         org.springframework.web.context.ContextLoaderListener     

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:/conf/context/spring-platform.xml 
    classpath:/conf/context/spring-beans.xml 
    classpath:/conf/context/spring-security.xml 
    </param-value>
</context-param>


<filter>
    <filter-name>accessFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>accessFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<servlet>
    <servlet-name>jersy</servlet-name>
    <!-- <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> -->
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <!-- <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> 
        <param-value>org.managers</param-value> </init-param> -->
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
        <!-- <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>org.filter.ServiceRequestFilter</param-value>   -->    
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersy</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
<jsp-config>
    <taglib>
        <taglib-uri>http://www.springframework.org/tags</taglib-uri>
        <taglib-location>/resources/tlds/spring.tld</taglib-location>
    </taglib>
</jsp-config>

注意到了(Locale没有改变)。日志或控制台中没有异常。

1 个答案:

答案 0 :(得分:6)

您正在使用 SessionLocaleResolver ,请尝试使用以下代码。

Locale locale = new Locale(lang);
WebUtils.setSessionAttribute(httpRequest,SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);

它会将您的语言环境设置为会话。我认为它会解决你的问题。