2014-01-24 79 views
1

我正在根据用户的语言选择不同的标签来显示不同的标签,但我似乎无法使其正常工作。无论何时更改servlet xml文件中的“defaultLocale”,它都会读取messages_en.properties和messages_de.properties文件。 ...?lang = en和...?lang = de根本不工作。Spring MVC国际化不起作用

<property name="defaultLocale" value="de" /> 

我的XML文件如下:

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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" version="3.0" metadata-complete="true"> 

    <display-name>SpringTest3</display-name> 

    <servlet> 
     <servlet-name>SpringTest3</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>SpringTest3</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
     /WEB-INF/application-context.xml 
    </param-value> 
    </context-param> 
</web-app> 

应用程序的context.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <context:component-scan base-package="com.qwerty.controllers" /> 
    <mvc:annotation-driven /> 

    <import resource="spring-datasource.xml" /> 
</beans> 

SpringTest3-sevlet.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" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="messages" /> 
    </bean> 

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

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

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> 
     <property name="interceptors"> 
      <list> 
       <ref bean="localeChangeInterceptor" /> 
      </list> 
     </property> 
    </bean> 
</beans> 
+0

您在根上下文(控制器上的''和* application-context.xml *内部的)中错误地定义了MVC处理。将它移动到servlet上下文(* SpringTest3-sevlet.xml *)并通过'>初始化locale拦截器,并删除不必要的处理程序映射定义(ControllerClassNameHandlerMapping)。 –

+1

我仍然有办法与春天,但我完全按照你的步骤,它的作品完美。 非常感谢你:) – Rikku121

+0

我会做出答案,然后:)。 –

回答

3

您错误地定义了根上下文中的MVC处理(控制器上的<context:component-scan><mvc:annotation-driven>内部的application-context.xml)。此举对servlet上下文(SpringTest3-sevlet.xml),并通过初始化区域设置拦截:

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

也放下你的不必要的处理程序映射定义(ControllerClassNameHandlerMapping)。

如果你不知道什么是的servlet上下文之间的差异,检查其他答案这个主题(例如Difference between applicationContext.xml and spring-servlet.xml in Spring Framework)。