Spring MVC - Themes with Internationalization

[Updated: Jan 11, 2018, Created: Jan 10, 2018]

Following example shows how to use internationalization capability which comes with Spring MVC Theme functionality. By default ResourceBundleThemeSource (implements ThemeSource) is used to load theme properties via a ResourceBundle. That means we can also add i18n based multiple theme properties files. One of them will be picked depending on the client Locale (based on 'Accept-Language' header value). Check out our MVC i18n tutorial as well.


Java Config class

In this example we are using default FixedThemeResolver.

Other than multiple i18n , we are also going to add some i18n based message files, for that we have to configure our own ResourceBundleMessageSource bean:

public class MyWebConfig implements WebMvcConfigurer {

  public MessageSource messageSource() {
      //this is only needed for text messages and not needed for theme internationalization
      ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
      return messageSource;

  public void addViewControllers(ViewControllerRegistry registry) {
      //mapping '/' to index view name without a controller
      ViewControllerRegistration r = registry.addViewController("/");

  public void configureViewResolvers(ViewResolverRegistry registry) {
      registry.jsp();//default prefix=/WEB-INF/", suffix=".jsp"

Theme Properties files


content-style=width:500px;border:solid 2px blue;margin:auto;padding:30px;


content-style=width:500px;border:solid 2px white;color:white;margin:auto;padding:30px;

Also note that by default, the theme's ResourceBundleThemeSource uses an empty base name prefix, so we don't need to use a prefix with theme properties files name. In the next example we will learn how to use a custom prefixes base name.

Message Properties files


app.msg = hi there


app.msg = salut

JSP View:


<%@ taglib prefix="spring" uri=""%>
<body style="background-color:<spring:theme code='background'/>;">
<div style="<spring:theme code='content-style'/>">
    <spring:message code="app.msg"/>

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


From browser with default locale of 'en-US':

Using Postman with an explicit 'Accept-Language' header value of 'fr-FR':

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 5.0.2.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Spring MVC Themes with Internationalization Example Select All Download
  • spring-mvc-theme-and-i18n
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • texts
        • webapp
          • WEB-INF

See Also