Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC国际化不起作用_Java_Spring_Jsp_Tomcat_Spring Mvc - Fatal编程技术网

Java SpringMVC国际化不起作用

Java SpringMVC国际化不起作用,java,spring,jsp,tomcat,spring-mvc,Java,Spring,Jsp,Tomcat,Spring Mvc,我试图让我的webapp根据用户选择的语言显示不同的标签,但我似乎无法让它正常工作。每当我更改servlet xml文件中的“defaultLocale”时,它都会读取messages_en.properties和messages_de.properties文件。。。?但是,lang=en和…?lang=de根本不起作用 <property name="defaultLocale" value="de" /> 我的xml文件如下所示: web.xml <?xml vers

我试图让我的webapp根据用户选择的语言显示不同的标签,但我似乎无法让它正常工作。每当我更改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>

春季测试3
春季测试3
org.springframework.web.servlet.DispatcherServlet
1.
春季测试3
/
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/application-context.xml
application-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>


您在根上下文中错误地定义了MVC处理(
在控制器上,而
在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上下文之间的区别,请检查有关此主题的其他答案(例如)。

您在根上下文中错误地定义了MVC处理(
在控制器上,而
在application context.xml内)。将其移动到servlet上下文(SpringTest3 sevlet.xml)并通过
初始化区域设置拦截器,并删除不必要的处理程序映射定义(
ControllerClassNameHandlerMapping
)。我仍然有办法使用Spring,但我完全遵循了您的步骤,它工作得非常完美。非常感谢:)那我就回答:)。