Spring MVC - Remembering User selected theme with SessionThemeResolver

[Last Updated: Jan 20, 2018]

SessionThemeResolver is similar to CookieThemeResolver (last example) but with one difference, that is user selected theme name is put in Servlet's HttpSession object on the server side instead of cookies on the browser side.


We are going to reuse our previous example and will replace CookieThemeResolver with SessionThemeResolver. There will be no other changes.

Java Config class

public class MyWebConfig implements WebMvcConfigurer {

  public ThemeResolver customThemeResolver() {
      SessionThemeResolver ctr = new SessionThemeResolver();
      return ctr;

  public void addInterceptors(InterceptorRegistry registry) {
      ThemeChangeInterceptor themeChangeInterceptor = new ThemeChangeInterceptor();

  public void configureViewResolvers(ViewResolverRegistry registry) {

Monitoring Session attribute changes

We are also going to use Servlet's HttpSessionAttributeListener implementation to see when theme name attribute is added to the session.

public class MySessionAttributeListener implements HttpSessionAttributeListener {

  public void attributeAdded(HttpSessionBindingEvent event) {
      print("attributeAdded", event);

  public void attributeRemoved(HttpSessionBindingEvent event) {
      print("attributeRemoved", event);

  public void attributeReplaced(HttpSessionBindingEvent event) {
      print("attributeReplaced: ", event);

  private void print(String msg, HttpSessionBindingEvent event) {
      HttpSession session = event.getSession();
      Object currentValue = null;
      try {
          currentValue = session.getAttribute(event.getName());
      } catch (IllegalStateException e) {
      System.out.printf("%s: name=%s, value=%s, currentValue=%s%n",
              msg, event.getName(), event.getValue(),


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

mvn tomcat7:run-war


The output is also same as the last example, except for no cookie will be sent to client browser, instead we will see following printed by our HttpSessionAttributeListener on the server console.

When we first select and submit the theme:

attributeAdded: name=org.springframework.web.servlet.theme.SessionThemeResolver.THEME, value=ocean-theme, currentValue=ocean-theme

Subsequent theme selection will give:

attributeReplaced: : name=org.springframework.web.servlet.theme.SessionThemeResolver.THEME, value=ocean-theme, currentValue=metal-theme
attributeReplaced: : name=org.springframework.web.servlet.theme.SessionThemeResolver.THEME, value=metal-theme, currentValue=ocean-theme

At the end of the session (e.g. on session timeout):

attributeRemoved: name=org.springframework.web.servlet.theme.SessionThemeResolver.THEME, value=ocean-theme, currentValue=null

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

SessionThemeResolver Example Select All Download
  • spring-session-theme-resolver
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • resources
          • webapp
            • WEB-INF
              • views

    See Also