Welcome to Part 6 of Spring 3.0 MVC Series. In previous article we saw how to add Internationalization i18n and Localization L10n support to Spring 3.0 based web application. We usedLocaleChangeInterceptor to intercept the change in locale andReloadableResourceBundleMessageSource class to add message resources properties.

In this part we will see how to add Themes in Spring MVC. We will create three different themes and add functionality in our HelloWorldSpring project for user to select any of the available theme. Once user selects a theme, we can save it in cookies so that it can be persisted between different sessions.

Introduction to Themes in Spring MVC

A theme is a collection of static resources, typically style sheets and images, that affect the visual style of the application. We can apply Spring Web MVC framework themes to set the overall look-and-feel of application, thereby enhancing user experience.

To use themes in your web application, you must set up an implementation of the org.springframework.ui.context.ThemeSource interface. The WebApplicationContext interface extends ThemeSource but delegates its responsibilities to a dedicated implementation. By default the delegate will be an org.springframework.ui.context.support.ResourceBundleThemeSource implementation that loads properties files from the root of the classpath. To use a custom ThemeSource implementation or to configure the base name prefix of the ResourceBundleThemeSource, you can register a bean in the application context with the reserved name themeSource. The web application context automatically detects a bean with that name and uses it.

When using the ResourceBundleThemeSource, a theme is defined in a simple properties file. The properties file lists the resources that make up the theme. Here is an example:

Our Goal

Our goal is to change the Hello World Spring 3 MVC and add Theme support to it. User will have option to select theme from 3 predefined themes (default, black and blue).

Adding Theme support to Spring 3 MVC

Let us configure our Spring 3 MVC application to add Theme support. For this we will add following code into spring-servlet.xml file.

File: WebContent/WEB-INF/spring-servlet.xml

<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean> <!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor"
class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<bean id="themeResolver"
class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default" />
</bean> <bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
<ref bean="themeChangeInterceptor" />
</list>
</property>
</bean>

In the above configuration, first we have added themeSource bean. Notice that this bean is an instance of class ResourceBundleThemeSource and we also have specified a property basenamePrefix with value“theme-“. ResourceBundleThemeSource class will load the property files containing theme definition starting with prefix “theme-“. Thus, if we have defined 3 new themes in our project (default, black and blue) then we will create 3 property files while will have certain configuration properties. Also these files will be placed under the project classpath.

Next, we defined an interceptor bean themeChangeInterceptor which is an instance of classorg.springframework.web.servlet.theme.ThemeChangeInterceptor. Also note here that we have specified a property paramName with value theme. This interceptor is invoked whenever a request is made with parameter “theme” with different values.

Once the themeChangeInterceptor intercepts the change in the theme, the changes are then stored in the cookies using class org.springframework.web.servlet.theme.CookieThemeResolver. We have configured this class in our spring-servlet.xml configuration file. Also note that we have specified default theme name with this bean.

Now create following properties files in resources/ folder of the project.

File: resources/theme-default.properties

css=themes/default.css

File: resources/theme-blue.properties

css=themes/blue.css

File: resources/theme-black.properties

css=themes/black.css

CSS Stylesheet for different Themes

Let us create 3 CSS stylesheet which will act as the theme files for our project. Create following CSS files in WebContent/themes folder.

File: WebContent/themes/default.css

body {
    background-color: white;
    color: black;
}

File: WebContent/themes/blue.css

body {
    background-color: #DBF5FF;
    color: #007AAB;
}

File: WebContent/themes/black.css

body {
    background-color: #888;
    color: white;
}

JSP View changes

We are almost done with the changes and last bit that is remaining is to add a functionality for user to select the theme from UI. For this we will change the header.jsp file and add 3 links with different themes. User can click on any of this link and change the theme of webapplication.

File: WebContent/WEB-INF/jsp/header.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
 
<h3><spring:message code="label.title"/></h3>
 
<span style="float: right">
    <a href="?lang=en">en</a>
    |
    <a href="?lang=de">de</a>
</span>
 
 
<span style="float: left">
    <a href="?theme=default">def</a>
    |
    <a href="?theme=black">blk</a>
    |
    <a href="?theme=blue">blu</a>
</span>

Notice that in above JSP changes we created 3 links with argument “?theme=”. Thus whenever user will click these links, a new parameter will be passed in the request with the appropriate theme. The request interceptor of Spring will fetch this value and change the theme accordingly.

That’s All Folks

That’s pretty much it :) We just added Theme support to our demo Spring 3.0 MVC application. All you have to do is just execute the app in Eclipse. Press Alt + Shift + X, R.

Download Source Code

Click here to download Source code (21kb, zip)

reference from:

http://viralpatel.net/blogs/spring-3-mvc-themes-tutorial-example/

Spring 3 MVC: Themes In Spring-Tutorial With Example---reference的更多相关文章

  1. Spring Web MVC框架简介

    Web MVC framework框架 Spring Web MVC框架简介 Spring MVC的核心是`DispatcherServlet`,该类作用非常多,分发请求处理,配置处理器映射,处理视图 ...

  2. Spring - Sring MVC入门

    2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...

  3. 12.Spring——Web MVC框架

    1.Spring Web MVC 框架 2.Spring MVC Hello World 例子 1.Spring Web MVC 框架 Spring web         MVC 框架提供了模型-视 ...

  4. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

    spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...

  5. spring 3 mvc hello world + mavern +jetty

    Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutori ...

  6. Gradle – Spring 4 MVC Hello World Example

    In this tutorial, we will show you a Gradle + Spring 4 MVC, Hello World Example (JSP view), XML conf ...

  7. Spring 3 MVC and JSON example

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  8. Features of Spring Web MVC

    21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution ...

  9. Spring 4 MVC example with Maven

    In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Sp ...

随机推荐

  1. 【算法】一般冒泡排序 O(n^2) 稳定的 C语言

    冒泡排序 一.算法描述 假设序列中有N个元素: 第一趟选取第一个元素作为关键字,从左至右比较,若遇到比它小的则放到它左边(也即两数进行交换),若遇到比它大的,则改为选取该元素作为关键字完成后续的比较, ...

  2. 在IT学习中的“认识论”

    先有“感性认识”,再有“理性认识”.这句经典认识论,在IT领域应用广泛. 一个项目,首先是分析需求,也就是功能层面,这相当于先有“感性认识”:然后才是具体的技术实现,也就是“理性认识”. IT学习分析 ...

  3. 【关于php】Appserv中关于DW配置站点问题

    用DW运行的话,还要配置下站点.或者你直接在浏览器地址栏上输入:http://localhost:8080/p5-1.php  或者是http://localhost/p5-1.php dreamwe ...

  4. 【POJ2396】Budget(上下界网络流)

    Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...

  5. Android+struts2+JSON方式的手机开发(Login)

    在手机的后台服务无论是调用WebService还是Http请求,多数都是采用Android的HttpClient实现相关的调用实现.本文实现Android+Struts2+JSON方式实现为手机前台提 ...

  6. 【HDOJ】3592 World Exhibition

    基础差分约束. /* 3592 */ #include <iostream> #include <algorithm> #include <queue> #incl ...

  7. Win7 64位系统U盘安装Centos6.5双系统

    (win764位旗舰版系统 Centos6.5亲测成功) 安装前准备: U盘.软碟通(UltraISO).CentOS-6.5-x86_64-bin-DVD1(DVD 2只是一些软件,安装系统只要DV ...

  8. 如何配置jdk和tomcat 转

    一.配置JDK1.解压JDK至D:\JDK1.5目录下(楼主可以自由选取目录).2.设置环境变量(右键我得电脑->属性->高级->环境变量),在系统变量中添加一个叫JAVA_HOME ...

  9. c语言 快速排序---归并排序----堆排序

    //快速排序: #include <stdio.h> #define MAX 500000 int s[MAX]; void Q_Sort(int start,int end) { int ...

  10. Cows - POJ 3348(凸包求面积)

    题目大意:利用n棵树当木桩修建牛圈,知道每头牛需要50平的生存空间,求最多能放养多少头牛. 分析:赤裸裸的求凸包然后计算凸包的面积. 代码如下: --------------------------- ...