0

In my app, I've made a switch for two languages (Eng\Rus) that must change the locale for the app. But, Spring LocaleResolver is not reacting to the request. This is a freemarker DropDown switch with "locale" var:

 <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>

This is my spring-servlet.xml config:

<!-- 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>

And web.xml config:

<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>

If I hard-code "defaultLocale" in "localeResolver" the page will load properly and all translation will be made properly. I clearly can see in fireBug that after switching locale, the locale changes in the page header. But, the content does not change on the page.

I have all needed configs that usualy needed:

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

Changes in LocalChangeInterseptor didn't give any changes:

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

One more thing; Does Spring-security have anything to do with Interceptor mechanism? Any help appreciated!!! Thank you.

4

2 回答 2

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>
于 2013-08-24T17:29:09.460 回答
0

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

 <mvc:annotation-driven />

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

-->

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

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

于 2013-08-24T18:37:51.493 回答