Svetoslav Marinov
Svetoslav Marinov

Reputation: 492

Spring MVC doesn't change locale from a link

I'm new in Spring MVC and I'm trying to add i18n in my site. I've the following configurations:

servlet-context.xml:

<beans:bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="language" />
</beans:bean>

<beans:bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
    <beans:property name="interceptors">
       <beans:list>
        <beans:ref bean="localeChangeInterceptor" />
       </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basename" value="messages" />
</beans:bean> 
<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>

And a controller:

@Controller
public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {        
    return "home";
}

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome(Locale locale, Model model) {
    return "WelcomePage";
}
}

The WelcomePage.jsp has the following content:

Language : <a href="?language=en">English</a>|<a href="?language=de_DE">German</a>

<h3>
    <spring:message code="welcome.springmvc" text="default text" />
</h3>

When I click on the links showed above the Locale doesn't change, although the URL of link becomes ../welcome?language=de_DE for example. If I add a controller like the following the WelcomePage.jsp changes the Locale correctly.

public class WelcomeController extends AbstractController{

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    ModelAndView model = new ModelAndView("WelcomePage");
    return model;
} 
}

How can I change the Locale when I use a Controller of the first type (with RequestMappping)?

Upvotes: 0

Views: 5574

Answers (1)

jmventar
jmventar

Reputation: 697

Local change interceptor should work properly with request parameters, first check your locale it's in your Jsp's using JSTL tag like this one.

${locale}

I think the problem is that it can't find any controller into ControllerClassNameHandlerMapping. Define the mappings manually:

<bean id="viewShoppingCart" class="x.y.z.ViewShoppingCartController">

Or use component scan and AnnotationMethodHandlerAdapter because you are using @Controller annotations.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:component-scan base-package="com.example.springmvc"/>

Even better check the following example configuration file for easier configuration using MVC simplification.

Then I would recommend you to declare a default locale in your xml configuration file if your default is not English and use a cookie to store locale.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


...

  <!-- Configures Handler Interceptors -->
  <mvc:interceptors>
    <!-- Changes the locale when a 'locale' request parameter is sent; e.g. 
    /?locale=de -->
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
  </mvc:interceptors>
  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="cookieName" value="myCookie" />
    <property name="defaultLocale" value="de" />
  </bean>
....
</beans>

Also put the ResourceBundleMessageSource with a valid path to messages eg : /WEB-INF/messages and the ViewResolver as yours.

Check official documentation: mvc simplification and official mvc 3.0 docs

Upvotes: 1

Related Questions