Locale in general requires a section of its own because of its growing significance in modern day application development. Having understood MessageSource in previous section and a bit of Locale, you will surely understand this section in much more detail and more concentrated on Locale.
In your application Locale can be chosen manually by your application code programmatically, selected by the browser in which your application runs or by fixing it using the various built-in LocaleResolver’s provided by Spring MVC as summarized below:
- springframework.web.servlet.i18n.AcceptHeaderLocaleResolver – simple locale resolver which checks the primary locale specified in the “accept-language” HTTP header send along with the client request.
- springframework.web.servlet.i18n.CookieLocaleResolver – It first tries to resolve the locale by sending back a cookie to the user’s machine and if it fails due to any reason, it looks the “accept-language” HTTP header to resolve it similar to AcceptHeaderLocaleResolver.
- springframework.web.servlet.i18n.SessionLocaleResolver – It first looks for a specific attribute in user’s session variable and if it fails looks into the specified default locale. As a fall back finally it looks into HTTP header “accept-language” and resolves the messages.
- springframework.web.servlet.i18n.FixedLocaleResolver – simplest LocaleResolver implementation which returns a fixed default locale, which is current JVM’s default locale.
Spring provides built-in interceptor which allows changing the current locale on every request by the client, via a configurable request parameter. Listing 14-20 below shows configuring this interceptor in the Spring configuration file.
Listing 14-20. Configuring LocaleChangeInterceptor in Spring configuration file
<property name="paramName" value="language"/>
In a web application themes are used to change look and feel of your web application in one single step by letting you point to different CSS, JSP, images etc. which are the core view enhancing components. Your web application should always be designed based on themes which can be customized and changed with minimal code changes and with minimal impact. Spring MVC allows you to do just about anything with the themes in your application with ease.
Spring achieves control over themes by using two important interfaces namely org.springframework.ui.context.ThemeSource and org.springframework.web.servlet.ThemeResolver. Spring does provide built-in implementation for both these interfaces. ThemeSource implementations provided by Spring MVC are:
- springframework.ui.context.support.ResourceBundleThemeSource – resolves themes by looking a ResourceBundle per theme used in your web application. The theme gets resolved by looking at the ResourceBundle base names. All theme elements are determined by following a common base name prefix.
Spring MVC does provide built-in ThemeResolver implementation, of which some of them can be as described below:
- springframework.web.servlet.theme.FixedThemeResolver – uses a fixed theme defined via the “defaultThemeName” property.
- springframework.web.servlet.theme.SessionThemeResolver – similar in concept to SessionLocaleResolver, in which theme attribute in user’s session will be used to resolve the theme and in case it is not defined fall back to the default theme.
- springframework.web.servlet.theme.CookieThemeResolver – similar in concept to CookieLocaleResolver, in which cookie send back to the user will be used to resolve the theme and in case it is not defined fall back to the default theme.
To ease configuration Spring MVC does have namespace coming to its rescue. The Spring MVC provides various confgurations, notable amoung them are summarized below:
- <mvc:annotation-driven> – Configures the annotation-driven Spring MVC Controller programming model.
- <mvc:interceptors> – ordered set of interceptors which will be executed before the request gets onto the controller. These interceptors allow requests to be pre/post processed, before/after handling.
- mvc:view-controller> – allows to define a simple controller which selects a view to render the response.
- <mvc:resources> – allows to configure handler which is used for serving static resources like images, js, css etc.
- <mvc:default-servlet-handler> – allows to configure handler for serving static resources by forwarding the request to the servlet container’s default configured servlet.
Page Visitors: 6143