博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 3 MVC: Themes In Spring-Tutorial With Example---reference
阅读量:7124 次
发布时间:2019-06-28

本文共 5646 字,大约阅读时间需要 18 分钟。

Welcome to Part 6 of Spring 3.0 MVC Series. In  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).

spring-mvc-theme-demo

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

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.

spring-mvc-theme-directory-structure

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.

spring-mvc-theme-demo

Download Source Code

 

reference from:

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

 

转载地址:http://ybael.baihongyu.com/

你可能感兴趣的文章
微信公众号开发小记——3.接入三方登录
查看>>
个推获7亿元C轮募资,SDK接入超80亿
查看>>
如何10分钟让APP实现实时互动?
查看>>
Consul入门01 - 安装Consul
查看>>
Swift 编程思想,第一部分(补遗):牺牲小马
查看>>
css 不规整元素的宽高等比例
查看>>
let vs const
查看>>
Swift里你可能不知道的事儿(2)——处理对象reference cycle的三种方式
查看>>
聊聊json
查看>>
【更新】FusionCharts图表库 v3.10.0发布|附下载
查看>>
Python async/await Tutorial(翻译)
查看>>
TensorFlow实战之K-Means聚类算法实践
查看>>
verify.js 极简表单校验插件
查看>>
调查显示,大多数 Java 开发人员不希望学习新语言
查看>>
华为诉美国政府案新进展,法院已发传票
查看>>
小米成立AIoT战略委员会,加速落地All in AIoT战略 ...
查看>>
全栈必备 JavaScript基础
查看>>
soamanager将RFC类型的FM发布成web service
查看>>
兰晟生物医药完成数千万元A轮融资,引领神经精神疾病创新药物的快速发展 ...
查看>>
中国 HBase 技术社区 2019 年全国 meetup 计划
查看>>