【问题标题】:Spring MVC internationalization not workingSpring MVC 国际化不起作用
【发布时间】:2014-02-15 21:06:27
【问题描述】:

我试图让我的 web 应用根据用户选择的语言显示不同的标签,但我似乎无法让它正常工作。每当我更改 servlet xml 文件中的“defaultLocale”时,它都会读取 messages_en.properties 和 messages_de.properties 文件。 ...?lang=en 和 ...?lang=de 根本不起作用。

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

应用程序上下文.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>

【问题讨论】:

  • 您在根上下文中错误地定义了 MVC 处理(控制器上的 &lt;context:component-scan&gt;application-context.xml 内的 &lt;mvc:annotation-driven&gt;)。将其移至 servlet 上下文 (SpringTest3-sevlet.xml) 并通过 &lt;mvc:interceptors&gt; 初始化语言环境拦截器并删除不必要的处理程序映射定义 (ControllerClassNameHandlerMapping)。
  • 我仍然有办法使用 Spring,但我完全按照您的步骤操作,并且效果很好。非常感谢:)
  • 我会从那做一个答案:)。

标签: java spring jsp tomcat spring-mvc


【解决方案1】:

您在根上下文中错误地定义了 MVC 处理(控制器上的 &lt;context:component-scan&gt;application-context.xml 内的 &lt;mvc:annotation-driven&gt;)。将其移至 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)。

如果您不确定 rootservlet 上下文之间有什么区别,请查看有关此主题的其他答案(例如 Difference between applicationContext.xml and spring-servlet.xml in Spring Framework )。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多