简体   繁体   English

Spring MVC国际化不起作用

[英]Spring MVC internationalization not working

I'm tring to make my webapp display different labels according to the user's language of choice, but I can't seem to make it work properly. 我正努力使我的webapp根据用户选择的语言显示不同的标签,但似乎无法使其正常运行。 It does read the messages_en.properties and messages_de.properties files whenever I change the "defaultLocale" in the servlet xml file. 每当我更改servlet xml文件中的“ defaultLocale”时,它都会读取messages_en.properties和messages_de.properties文件。 ...?lang=en and ...?lang=de are not working at all though. ...?lang = en和...?lang = de根本不起作用。

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

My xml files are as follows: 我的xml文件如下:

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

application-context.xml 应用程序的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 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>

You are incorrectly defining MVC handling in your root context ( <context:component-scan> on controllers and <mvc:annotation-driven> inside application-context.xml ). 您在根上下文(在控制器上的<context:component-scan>application-context.xml中的 <mvc:annotation-driven> )中错误地定义了MVC处理。 Move that to the servlet context ( SpringTest3-sevlet.xml ) and initialize locale interceptor via : 将其移至servlet上下文( SpringTest3-sevlet.xml )并通过以下方式初始化语言环境拦截器:

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

Also drop your unnecessary handler mapping definition ( ControllerClassNameHandlerMapping ). 还要删除不必要的处理程序映射定义( ControllerClassNameHandlerMapping )。

If you are not sure what are the differences between root and servlet contexts, check other answers on this topic (eg Difference between applicationContext.xml and spring-servlet.xml in Spring Framework ). 如果不确定上下文和servlet上下文之间有什么区别,请检查有关此主题的其他答案(例如,Spring Framework中的applicationContext.xml和spring-servlet.xml之间的区别 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM