9

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Vadim Chekry
  • 1,243
  • 2
  • 12
  • 15

4 Answers4

21

<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.

Community
  • 1
  • 1
kurochenko
  • 1,214
  • 3
  • 19
  • 43
6

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.

zero323
  • 322,348
  • 103
  • 959
  • 935
user1250852
  • 191
  • 1
  • 3
  • 7
0

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>
0

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>