Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Locales in Spring MVC

In this section, you will learn about Locales in Spring MVC.


Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller

Locales in Spring MVC

In this section, you will learn about Locales in Spring MVC.

Set of parameters that determines the language, country and any preference of the user in a user interface is known as a locale.

Spring MVC also support internationalization and localization. In Spring MVC,  DispatcherServlet allows you to automatically resolve messages by using client's locale. This can be done using  LocaleResolver objects.

When a request came, DispatcherServlet searches for a locale resolver. You can get the locale resolved by the locale resolver using the method RequestContext.getLocale().

For changing the locale under particular circumstances, you can also add interceptor to the handler mapping. For example, based on some request parameter, you can change locale.

Given below the Local resolvers in the Spring :


The client' s request header accept-language is read by this locale resolver to resolve the locale. Generally, client's operating system locale is held by this header field.


This locale resolver inspects the cookies for finding the locale. If a local is specified in the cookie, it uses this locale.

Also, using the property of the locale resolver, you can set the name of the cookie in addition to the maximum age. You can define CookieLocaleResolver as follows :

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
	<property name="cookieName" value="locallang"/>
	<property name="cookieMaxAge" value="100000">

Properties of CookieLocaleResolver

The description of the properties of CookieLocaleResolver is given below :

Property Name Default Value Description
cookieName classname + LOCALE This property is used to set the cookie name.
cookieMaxAge Integer.MAX_INT This property's value is in seconds. It is the maximum time a cookie will live on the client system.
cookiePath / This property is used to confined the visibility to specific parts of your site. When the cookiePath is set, it will be visible to that path and the path below it.


This local resolver is used to get the locales from the session which might be related to the request from the user.


The below example will show you that calls to all *.view resources containing a parameter named UserLang will now change the locale. For example, the following request - will change the language to German.

<bean id="localeChangeInterceptor"
	<property name="paramName" value="UserLang"/>

<bean id="localeResolver"

<bean id="urlMapping"
	<property name="interceptors">
			<ref bean="localeChangeInterceptor"/>
	<property name="mappings">


Share on Google+Share on Google+

Locales in Spring MVC

Posted on: January 11, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Locales in Spring MVC  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image