Close

Spring MVC - Remembering User selected theme with SessionThemeResolver

[Updated: Jan 20, 2018, Created: 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.

Example

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

Java Config class

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {

  @Bean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME)
  public ThemeResolver customThemeResolver() {
      SessionThemeResolver ctr = new SessionThemeResolver();
      ctr.setDefaultThemeName(ThemeInfo.DefaultThemeInfo.getThemeName());
      return ctr;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
      ThemeChangeInterceptor themeChangeInterceptor = new ThemeChangeInterceptor();
      themeChangeInterceptor.setParamName("themeName");
      registry.addInterceptor(themeChangeInterceptor);
  }

  @Override
  public void configureViewResolvers(ViewResolverRegistry registry) {
      registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp");
  }
}

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.

@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener {

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

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

  @Override
  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(),
              currentValue);

  }
}

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

mvn tomcat7:run-war

Output

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