Theme Resolver Example in Spring MVC with annotation configuration (Spring 3.x)

Previous tutorial make us understand, the way to implement theme resolver in Spring MVC application with xml configuration. But, what if we do working with annotations? In this tutorial we will learn the implementation of spring theme resolver in annotation configuration.

It is recommended to read Theme Resolver Example in Spring MVC with xml configuration (Spring 2.5) tutorial, so that you can understand basic implementation of theme resolver.

This tutorial will only illustrate the configuration changes that we have to made for using annotation based spring mvc configuration.

For making our example comfortable with annotation configuration, we just have to make two changes:

  1. Change the controller class ( to adopt annotations.
  2. Change the spring mvc configuration file to activate annotation driven configuration and implement theme resolver with it.

Configuration changes made in spring mvc configuration file (app-config.xml)

Following is the changed configuration file that will make annotation driven configuration active and implement spring theme resolver with it:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<!-- Application Message Bundle -->
<bean id="messageSource" class="">
    <property name="basename" value="/WEB-INF/messages" />
    <property name="cacheSeconds" value="3000" />

<bean id="themeSource" class="">
    <property name="basenamePrefix" value="theme-" />

<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <property name="paramName" value="theme" />
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <property name="defaultThemeName" value="default" />

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
            <ref bean="themeChangeInterceptor" />

<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.raistudies" />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>


We have just registered DefaultAnnotationHandlerMapping as handler mapping in place of BeanNameUrlHandlerMapping and included the <context:component-scan base-package=”com.raistudies” /> tag component scan.

Changes to controller class (

package com.raistudies.controllers;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.raistudies.domain.RegistrationBean;

public class RegistrationController {

    public String showForm(ModelMap model){
        RegistrationBean userRegis = new RegistrationBean();
        model.addAttribute("Registration", userRegis);
        return "registration";

    public String processForm(@ModelAttribute(value="Registration") @Valid RegistrationBean userRegis,BindingResult result){
            return "registration";
            return "success";

The controller is just being changed to work with annotation driven configurations.

That is all and you will get same result as you get in previous tutorial.

You can download the code of this example and try it yourself from bellow links:

Source: Download

Source + lib:Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>