Spring 的本地化不会切换语言

发布于 2025-01-08 07:16:31 字数 4463 浏览 0 评论 0原文

主题在主题中 - 我无法弄清楚 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.propertiesmessages_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还在原地等你 2025-01-15 07:16:31

覆盖 XML 配置中定义的 LocaleChangeInterceptor。尝试添加此内容(根据 spring 参考)到 XML 配置:

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

或者尝试摆脱 ,这在 此处

<mvc:annotaion-driven /> overrides LocaleChangeInterceptor defined in your XML config. Try to add this (according to spring reference) to XML config:

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

or try to get rid of <mvc:annotaion-driven />, which is discussed here.

后eg是否自 2025-01-15 07:16:31

这对我也有用。其他人注意:我可能是错的,但使用 Spring MVC 3.1 时似乎需要 mvc:interceptors 。另请注意,使用 mvc:interceptors 时,请确保您没有 handlermapping 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小时沮丧的根源。

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 using mvc:interceptors make sure you DO NOT have the handlermapping bean:

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

Having this bean causes the error:

"Cannot resolve reference to bean 'localeChangeInterceptor' while setting bean property 'interceptors'"
This was the source of my 8 hour frustration.

比忠 2025-01-15 07:16:31

我遇到了同样的问题,但我通过在拦截器中引用 localeChangeInterceptor bean 来解决......它的工作完美。

    <mvc:interceptors>
            <beans:ref bean="localeChangeInterceptor"/>
    </mvc:interceptors>

I had a same problem but I solved by just refer localeChangeInterceptor bean in interceptors...its works perfect.

    <mvc:interceptors>
            <beans:ref bean="localeChangeInterceptor"/>
    </mvc:interceptors>
遗心遗梦遗幸福 2025-01-15 07:16:31

当我们写:
它注册了一个RequestMappingHandlerMapping、一个RequestMappingHandlerAdapter和一个ExceptionHandlerExceptionResolver(以及许多其他东西),以支持使用@RequestMapping、@等注解来处理带有带注释的控制器方法的请求。异常处理程序等。
它还提供了 SimpleUrlHandlerMapping 的默认实现。如果你想覆盖它的默认实现 - 在你的情况下你想为它提供一些拦截器,你可以通过注册你的拦截器 bean 来做到这一点,如下所示:

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

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:

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文