Spring MVC 3 localeChangeInterceptor

依然范特西╮ 提交于 2019-12-08 08:59:35

问题


I want to add Internationalization to my Spring MVC application. I know it can be a copy question but I have tried all solutions from both stackoverflow and google but still could not get working. I will explain in details. Here is

web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->


    <!-- Localization Start -->

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="basenames">
            <list>
                <value>/WEB-INF/messages</value>
            </list>
        </property>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="tr" />
    </bean>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

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


    <!-- Localization End -->   

</beans>

I have messages_en.properties and messages_tr.properties files. And they are working true, because when I change defaultLocale to en or tr , I get true labels from properties file. But the problem is that when I try from browser ?lang=tr or ?lang=en nothing changes.

Thanks for attention!


回答1:


In case you are using spring security put your LocaleChangeInterceptor inside mvc:interceptors tag like

  <mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
  </mvc:interceptors>

and remove handlerMapping bean from configuration.Hope it will work.



来源:https://stackoverflow.com/questions/18287711/spring-mvc-3-localechangeinterceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!