Spring LocaleResolver没有对语言环境做出反应

时间:2013-08-24 15:51:29

标签: java spring-mvc localization

在我的应用中,我已经为两种语言(Eng \ Rus)进行了切换,必须更改应用的区域设置。但是,Spring LocaleResolver没有对请求作出反应。这是一个带有“locale”var:

的freemarker DropDown开关
 <ul class="nav">
   <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" ><@spring.message "header.lang_switch" /><b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="?locale=en_US"><@spring.message "header.lang_switch_eng" /></a></li>
            <li><a href="?locale=ru_RU"><@spring.message "header.lang_switch_ru" /></a></li>
        </ul>
   </li>
</ul>

这是我的spring-servlet.xml配置:

<!-- serving up static resources -->
<mvc:resources mapping="/static/**" location="/static/"/>

<!-- Configures Handler Interceptors -->
<bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
      p:paramName="locale"/>

<!-- locale Resolver configuration-->
<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <!--<property name="defaultLocale" value="en_US"/>-->
</bean>

<bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor"/>
        </list>
    </property>
</bean>

<!-- Application Message Bundle -->
<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="fileEncodings" value="UTF-8"/>
    <property name="cacheSeconds" value="0"/>
    <property name="fallbackToSystemLocale" value="false"/>
    <property name="useCodeAsDefaultMessage" value="true"/>
    <property name="basenames">
        <list>
            <value>/WEB-INF/messages/messages</value>
            <value>/WEB-INF/messages/labels</value>
            <value>/WEB-INF/messages/include</value>
        </list>
    </property>
</bean>

和web.xml配置:

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- ===================== i18n configurations =============================================== -->
<filter>
    <filter-name>localizationFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>localizationFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

如果我在“localeResolver”中硬编码“defaultLocale”,页面将正确加载并且所有翻译都将正确进行。我清楚地可以在fireBug中看到,在切换语言环境后,语言环境会在页眉中发生变化。但是,内容在页面上不会改变。

我需要通常需要的配置:

<context:annotation-config/>
<context:component-scan base-package="serverMonitoring"/>
<mvc:annotation-driven />

LocalChangeInterseptor中的更改未提供任何更改:

<mvc:interceptors>
    <bean id="localeChangeInterceptor"
          class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
          p:paramName="locale"/>
</mvc:interceptors>

还有一件事; Spring-security是否与Interceptor机制有关? 任何帮助赞赏!谢谢。

2 个答案:

答案 0 :(得分:1)

我的配置正在运行,我们之间最大的区别是我使用'拦截器'标签:

<!-- Interceptors for theming support -->
<interceptors>
    <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" 
        p:paramName="lang"/>

</interceptors>

<!-- Beans for i18n support  -->
<beans:bean
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
    id="messageSource" p:basenames="WEB-INF/i18n/view,WEB-INF/i18n/presentation,WEB-INF/i18n/projects,WEB-INF/i18n/forms,WEB-INF/i18n/validation"
    p:fallbackToSystemLocale="false" />
<beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
    id="localeResolver" p:cookieName="locale" />

以我的方式,它可以为你工作。它直接来自“Pro Spring 3”一书。

编辑:这是我的web.xml,即使我从未对i18n支持做出改变

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

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


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
        /WEB-INF/spring/security-config.xml
        /WEB-INF/spring/datasource-context.xml
        /WEB-INF/spring/*-tx-config.xml
        /WEB-INF/spring/*-service-context.xml
    </param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Spring security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

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

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

答案 1 :(得分:0)

我解决了这个问题!为了解决它,我再次将localeChangeInterceptor移动到tag。我这样做是因为使用了覆盖handlerMapping(主题在StacOverflow中)并且我将所有mvc配置移动到/ resource folger(app的服务器端)

 <mvc:annotation-driven />

<!--
  Configures the location of static resources such as css files.
  Requires Spring Framework 3.0 or higher.

- &GT;     

<!-- Configures Handler Interceptors -->
<mvc:interceptors>
    <bean id="localeChangeInterceptor"
          class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
          p:paramName="lang"/>
</mvc:interceptors>

它开始工作了!感谢@sam与我分享您的配置。