2012-02-22 11 views
8

トピックが対象です - Spring MVCアプリケーションでロケールの切り替えに何が問題であるかわかりません。チュートリアルとして私はそれを使用していたlink +私はgoogleで見つけたさまざまなバリエーションを試してみました。言語を変更するために私のWebページのリンクをクリックすると、文字列?lang=XXがアドレスに追加されていますが、何も起こりません。Springのローカリゼーションで言語が切り替わらない

は、ここに私のサーブレットの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を持っています。どうやら、私はいくつかの詳細を欠場しているが、私は間違いなく問題をキャッチすることはできません。ところで、<beans:property name="defaultLocale" value="en"/>の値を変更しているときに、言語が正しく変更されます。私は本当にあなたの助けに感謝します。

答えて

21

<mvc:annotaion-driven />オーバーライドLocaleChangeInterceptorは、XML設定で定義されています。 XML設定ファイルには、この(spring referenceによる)を追加しよう:

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

またはhere議論され、<mvc:annotaion-driven />を取り除くためにしてみてください。

+0

コードのブロックは私のために働いたと付け加え、ありがとう: - あなたがそのデフォルトimplementaionを上書きしたい場合は、あなたのケースでは、いくつかのインターセプタでそれを提供したい、あなたはこのようなあなたのインターセプターBeanを登録することでそれを行うことができます! –

+0

私のために働いた!ありがとう –

+0

私のために働いた...! – madhairsilence

6

これは私にとってもうまくいきました。他の人への注意:間違っているかもしれませんが、Spring MVC 3.1を使用する場合はmvc:interceptorsが必要です。あなたはhandlermapping Beanを持っていないことを確認してくださいmvc:interceptorsを使用する場合にも、注意してください。

このBeanは、エラーが発生持つ
<bean id="handlerMapping" 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
    <property name="interceptors"> 
     <ref bean="localeChangeInterceptor" /> 
    </property> 
</bean> 

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

+0

答えに加えてくれてありがとう、私は同じ問題を抱えていました – maxivis

+0

良いヒント、あなたの答えをありがとう。 –

+0

ハンドラマッピングBeanを削除すると、そのトリックが実行されました。どうも。 – katzenhut

0

私は同じ問題を抱えていたが、私はちょうどlocaleChangeInterceptor Beanを参照してくださいすることで解決インターセプタで...その作品は完璧です。

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

我々は書き込み: <mvc:annotation-driven />そのような@RequestMapping、@ExceptionHandlerなどとして注釈を使用して注釈付き制御方法と処理要求のサポートにRequestMappingHandlerMapping、RequestMappingHandlerAdapter及びExceptionHandlerExceptionResolver(および他の多くのもの)を登録します。 SimpleUrlHandlerMappingのデフォルトの実装も提供します。

<mvc:interceptors> 
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
    <property name="paramName" value="lang" /> 
</bean> 
</mvc:interceptors> 
関連する問題