2012-02-22 1 views
8

L'argomento è nell'argomento - Non riesco a capire qual è il problema con il passaggio da locale all'applicazione Spring MVC. Come tutorial stavo usando quello link + ho provato diverse varianti che ho trovato su google. Quando faccio clic sui collegamenti della mia pagina Web per cambiare la lingua, la stringa ?lang=XX viene aggiunta all'indirizzo, ma non accade nulla.La localizzazione di Spring non cambia lingua

Ecco il mio 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> 

mio regolatore:

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> 

E devo messages_en.properties e messages_ru.properties nella mia directory src/main/resources. Apparentemente, ho perso alcuni dettagli, ma sicuramente non riesco a cogliere il problema. A proposito, quando sto cambiando il valore in <beans:property name="defaultLocale" value="en"/> le lingue cambiano correttamente. Apprezzerei molto il tuo aiuto.

risposta

21

<mvc:annotaion-driven /> annullamenti LocaleChangeInterceptor definiti nella configurazione XML. Cercate di aggiungere questo (secondo spring reference) a configurazione XML:

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

o cercare di sbarazzarsi di <mvc:annotaion-driven />, di cui si parla here.

+0

Grazie, aggiungendo che blocco di codice ha funzionato per me ! –

+0

ha funzionato anche per me !! grazie –

+0

ha lavorato per me per ...! – madhairsilence

6

Questo ha funzionato anche per me. Nota per gli altri: potrei sbagliarmi, ma sembra che sia richiesto mvc:interceptors quando si utilizza Spring MVC 3.1. Si noti inoltre, quando si utilizza mvc:interceptors assicurarsi che non hanno il fagiolo handlermapping:

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

Avere questo fagiolo causa l'errore:

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

+0

Grazie per questa aggiunta alla risposta, stavo avendo lo stesso problema – maxivis

+0

Buon suggerimento, grazie per la risposta. –

+0

rimozione del gestore Il bean di elaborazione ha fatto il trucco. grazie. – katzenhut

0

ho avuto uno stesso problema, ma ho risolto semplicemente fare riferimento localeChangeInterceptor fagioli negli intercettori ... funziona perfettamente.

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

Quando si scrive: <mvc:annotation-driven /> Si registra una RequestMappingHandlerMapping, un RequestMappingHandlerAdapter e un ExceptionHandlerExceptionResolver (e molte altre cose) a sostegno delle domande di elaborazione con metodi controllore annotati utilizzando annotazioni come @RequestMapping, @ExceptionHandler e altri. Fornisce anche l'implementazione predefinita per SimpleUrlHandlerMapping. Se si desidera ignorare la sua implementaion predefinita - nel vostro caso si vuole fornire con qualche intercettore, è possibile farlo registrando il bean intercettore in questo modo:

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