Spring 的本地化不会切换语言
主题在主题中 - 我无法弄清楚 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>
我的控制器:
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>
我的 src/main/resources
中有 messages_en.properties
和 messages_ru.properties
代码>目录。显然,我错过了一些细节,但我绝对无法抓住问题所在。顺便说一句,当我更改
中的值时,语言会正确更改。我非常感谢你的帮助。
The subject is in the topic - I can't figure out what is the problem with locales switching in my Spring MVC application. As a tutorial I was using that link + I've tried different variations I've found in google. When I click on my web page links to change the language the string ?lang=XX
is being appended to the address, but nothing happens.
Here is my 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>
My controller:
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>
And I have messages_en.properties
and messages_ru.properties
in my src/main/resources
directory. Apparently, I've missed some details, but I definitely can't catch the problem. BTW, when I am changing the value in <beans:property name="defaultLocale" value="en"/>
the languages change properly. I would really appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
覆盖 XML 配置中定义的LocaleChangeInterceptor
。尝试添加此内容(根据 spring 参考)到 XML 配置:或者尝试摆脱
,这在 此处。<mvc:annotaion-driven />
overridesLocaleChangeInterceptor
defined in your XML config. Try to add this (according to spring reference) to XML config:or try to get rid of
<mvc:annotaion-driven />
, which is discussed here.这对我也有用。其他人注意:我可能是错的,但使用 Spring MVC 3.1 时似乎需要
mvc:interceptors
。另请注意,使用mvc:interceptors
时,请确保您没有 handlermapping bean:拥有此 bean 会导致错误:
This worked for me as well. Note to others: I may be wrong, but it seems that
mvc:interceptors
is required when using Spring MVC 3.1. Also note, when usingmvc:interceptors
make sure you DO NOT have the handlermapping bean:Having this bean causes the error:
我遇到了同样的问题,但我通过在拦截器中引用 localeChangeInterceptor bean 来解决......它的工作完美。
I had a same problem but I solved by just refer localeChangeInterceptor bean in interceptors...its works perfect.
当我们写:
它注册了一个RequestMappingHandlerMapping、一个RequestMappingHandlerAdapter和一个ExceptionHandlerExceptionResolver(以及许多其他东西),以支持使用@RequestMapping、@等注解来处理带有带注释的控制器方法的请求。异常处理程序等。它还提供了 SimpleUrlHandlerMapping 的默认实现。如果你想覆盖它的默认实现 - 在你的情况下你想为它提供一些拦截器,你可以通过注册你的拦截器 bean 来做到这一点,如下所示:
When we write:
<mvc:annotation-driven />
It registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter and an ExceptionHandlerExceptionResolver (and many other things) in support of processing requests with annotated controller methods using annotations such as @RequestMapping, @ExceptionHandler and others.It also provides default implementation for SimpleUrlHandlerMapping. If you want to override its default implementaion - in your case you want to provide it with some interceptor, you can do that by registering your interceptor bean like this: