【问题标题】:Spring's localization doesn't switch languagesSpring 的本地化不会切换语言
【发布时间】:2012-03-13 03:15:18
【问题描述】:

主题在主题中——我不知道在我的 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>

我的src/main/resources 目录中有messages_en.propertiesmessages_ru.properties。显然,我错过了一些细节,但我绝对无法发现问题。顺便说一句,当我更改 &lt;beans:property name="defaultLocale" value="en"/&gt; 中的值时,语言会正确更改。非常感谢您的帮助。

【问题讨论】:

    标签: java spring-mvc localization


    【解决方案1】:

    &lt;mvc:annotaion-driven /&gt; 覆盖在 XML 配置中定义的 LocaleChangeInterceptor。尝试将此(根据spring reference)添加到 XML 配置中:

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

    或尝试摆脱&lt;mvc:annotaion-driven /&gt;,这是在讨论here

    【讨论】:

    • 它有效,但我还需要@zero323 的答案
    【解决方案2】:

    这也对我有用。其他人注意:我可能错了,但使用 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 属性 'interceptors' 时无法解析对 bean 'localeChangeInterceptor' 的引用” 这是我 8 小时沮丧的根源。

    【讨论】:

    • 感谢您对答案的补充,我遇到了同样的问题
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多