Spring MVC - Customizing Theme name with FixedThemeResolver

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

Following example will show how to customize ThemeResolver. We are still going to use FixedThemeResolver (used by default as well) but with a different theme name.


Java Config class

DispatcherServlet looks for user configured ThemeResolver bean by a specific name as defined by DispatcherServlet.THEME_RESOLVER_BEAN_NAME before falling back to the default one:

public class MyWebConfig implements WebMvcConfigurer {

  public ThemeResolver customThemeResolver() {
      FixedThemeResolver fixedThemeResolver = new FixedThemeResolver();
      return fixedThemeResolver;

  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 Property file


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

JSP View:


<%@ taglib prefix="spring" uri=""%>
<body style="background-color:<spring:theme code='background'/>;">
<div style="<spring:theme code='content-style'/>">
    Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
    Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.
    Donec eu libero sit amet quam egestas semper. Aenean ultricies
    mi vitae est. Mauris placerat eleifend leo.

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

mvn tomcat7:run-war


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 Customizing theme name with FixedThemeResolver Select All Download
  • spring-mvc-customized-theme-name
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
        • webapp
          • WEB-INF

See Also