2011-05-25 79 views
10

编辑链接:我的Spring框架版本3.0.5Spring MVC的3区域设置改变使用不工作

这里一个小问题,当我点击语言转换链接的语言没有改变。

语言文件(messages_xx.properties)位于classpath i18n目录中。这些文件是:

i18n/messages_en.properties 
i18n/messages_ar.properties 

Spring配置文件

<!-- Component scanner. This is used to automatically find Spring annotations like @Service and @Repository --> 
    <context:component-scan base-package="com.keype" /> 

    <!-- Annotation driven programming model --> 
    <mvc:annotation-driven /> 
    <context:annotation-config /> 
    <mvc:resources mapping="/static/**" location="/static/" /> 


    <!-- Session Object Configuration --> 
    <bean id="session" class="com.keype.system.Session" scope="session"> 
     <aop:scoped-proxy /> 
    </bean> 

    <!-- The View Resolver --> 
    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" 
      /> 

    <!-- i18n Configuration. Default language is english. Change language using ?language=en --> 
    <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.SessionLocaleResolver"> 
     <property name="defaultLocale" value="en" /> 
    </bean> 

    <!-- Message text files. This is set UTF-8 to display Arabic UTF correctly. -->  
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:i18n/messages" /> 
     <property name="defaultEncoding" value="UTF-8"/> 
    </bean> 

从JSP代码

<a href="?lang=ar"><spring:message code="header.arabic" /></a> | 
    <a href="?lang=en"><spring:message code="header.english" /></a> 

问题是一个部分,当我点击上面的链接来改变语言,语言环境更改功能不起作用。我通过将“defaultLocate”更改为“ar”进行测试,并获得了阿拉伯语文本。

这里有什么可能是错的?在tomcat日志中也没有任何东西。

回答

29

您必须在Spring MVC的MVC拦截器中注册localeChangeInterceptor才能考虑它。拦截器添加到配置:

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

我现在试过了。但是我收到异常:org.xml.sax.SAXParseException:cvc-complex-type.2.4.a:发现从元素'ref'开始的内容无效。 “{”http://www.springframework.org/schema/beans":bean,“http://www.springframework.org/schema/mvc":interceptor}”之一是预期的。顺便说一句,在我提到的关于拦截器注册的任何教程中,都没有什么奇怪的地方。有什么想法吗? – 2011-05-26 00:45:54

+0

我想这太: 没有运气 – 2011-05-26 01:02:32

+0

你是对的,元素不允许。 ¿是否包含编辑答案中显示的lang param? – 2011-05-26 08:16:35

1

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

<bean id="localeResolver" 
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
    <property name="defaultLocale" value="en" /> 
</bean> 
0

另一件事,可以帮助别人:

在我的情况,我必须在applicationContext.xml。把它放在spring-servlet(ref。dispatcher)中,根本没有工作。