主题在主题中 - 我无法弄清楚我的Spring MVC应用程序中的语言环境切换有什么问题。作为我使用link +的教程,我尝试了谷歌中发现的不同变体。当我点击我的网页链接更改语言时,字符串?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>
我的控制器:
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";
}
}
的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>
我的messages_en.properties
目录中有messages_ru.properties
和src/main/resources
。显然,我错过了一些细节,但我肯定无法解决问题。顺便说一句,当我更改<beans:property name="defaultLocale" value="en"/>
中的值时,语言会正常更改。我非常感谢你的帮助。
答案 0 :(得分:21)
<mvc:annotaion-driven />
会覆盖XML配置中定义的LocaleChangeInterceptor
。尝试将此(根据spring reference)添加到XML配置:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
或试图摆脱<mvc:annotaion-driven />
,这是here。
答案 1 :(得分:6)
这对我也有用。别人注意:我可能错了,但在使用Spring MVC 3.1时似乎需要mvc:interceptors
。另请注意,使用mvc:interceptors
时请确保您没有处理程序映射bean:
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
拥有此bean会导致错误:
“设置bean属性'拦截器'时无法解析对bean'localeChangeInterceptor'的引用” 这是我8小时挫折的根源。
答案 2 :(得分:0)
我有同样的问题,但我通过在拦截器中引用localeChangeInterceptor bean来解决它...它的工作完美。
<mvc:interceptors>
<beans:ref bean="localeChangeInterceptor"/>
</mvc:interceptors>
答案 3 :(得分:0)
当我们写:
<mvc:annotation-driven />
它使用注释(如@RequestMapping,@ ExceptionHandler等)注册RequestMappingHandlerMapping,RequestMappingHandlerAdapter和ExceptionHandlerExceptionResolver(以及许多其他内容),以支持使用带注释的控制器方法处理请求。
它还为SimpleUrlHandlerMapping提供默认实现。如果你想覆盖它的默认实现 - 在你的情况下你想为它提供一些拦截器,你可以通过注册你的拦截器bean来做到这一点:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>