Spring的本地化不会切换语言

Vad*_*kry 8 java localization spring-mvc

主题是在主题中 - 我无法弄清楚我的Spring MVC应用程序中的语言环境切换有什么问题.作为一个教程,我正在使用该链接 +我尝试了在谷歌中发现的不同变体.当我点击我的网页链接更改语言时,字符串?lang=XX将被附加到地址,但没有任何反应.

这是我的servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="ua.dod.picload.web" />

<!-- Internalization and localization support -->
<beans:bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:message" />
    <beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>
<beans:bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <beans:property name="interceptors">
        <beans:ref bean="localeChangeInterceptor" />
    </beans:property>
</beans:bean>
<beans:bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <beans:property name="defaultLocale" value="en"/>
</beans:bean>


</beans:beans>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

package ua.dod.picload.web;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

@RequestMapping("/index")
public String listIndex(Map<String, Object> map) {
    return "index";
}

@RequestMapping("/")
public String home() {
    return "redirect:/index";
}

}
Run Code Online (Sandbox Code Playgroud)

的index.jsp

<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <title><spring:message code="label.title" /></title>
</head>
<body>

    <span style="float: right">
    <a href="?lang=en">en</a>
    <a href="?lang=ru">ru</a>
    </span>

    <spring:message code="label.body" />

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

messages_en.propertiesmessages_ru.properties我的src/main/resources目录.显然,我错过了一些细节,但我肯定无法解决问题.顺便说一句,当我改变<beans:property name="defaultLocale" value="en"/>语言中的值时,正确地改变了.我将衷心感谢您的帮助.

kur*_*nko 21

<mvc:annotaion-driven />LocaleChangeInterceptorXML配置中定义的覆盖.尝试将此(根据spring参考)添加到XML配置:

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)

或者试图摆脱<mvc:annotaion-driven />,这里讨论.


小智 6

这对我也有用.别人注意:我可能错了,但是mvc:interceptors在使用Spring MVC 3.1时似乎是必需的.另请注意,使用时请mvc:interceptors确保您没有handlermapping bean:

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

有这个bean会导致错误:

"在设置bean属性'拦截器'时无法解析对bean'localeChangeInterceptor'的引用"这是我8小时挫折的根源.