Friday 26 July 2013

Spring Theme Resolver [Cookies Theme Resolver]

Spring Theme Resolver Features


Hi Guys,

This time , I am posting the blog on the one of features of spring , that makes your application for more user attractive . So I would like to share my " Spring Theme Resolver Concept ", so that it become easier for you guys to work on that.

Spring ThemeResolver is very nice concept of Spring , generally it's main objective to put the style sheet for your java web application dynamically. ThemeResolver uses the following three way to implement this , these are----
1) Fixed ThemeResolver
2) Cookies ThemeResolver
3) Session ThemeResolver

From the above ways , the second and third are used frequently , while first one is not used so much.

I will Start from the CookiesThemeResolver ,before going to explain I would like to tell you the dependencies needed for this-------

Jar Dependencies:
    common-logging-1.1.1jar
    jstl.jar
    spring-asm-3.1.1.RELEASE.jar
    spring-beans-3.1.1.RELEASE.jar
    spring-context-3.1.1.RELEASE.jar
    spring-context-support-3.1.1.RELEASE.jar
    spring-core-3.1.1.RELEASE.jar
    spring-expression-3.1.1.RELEASE.jar
    spring-web-3.1.1.RELEASE.jar
    spring-webmvc-3.1.1.RELEASE.jar

You guys need to follow the below steps

1) Configuration of Spring Configuration file

<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>

<mvc:interceptors>
<ref bean="themeChangeInterceptor" />

</mvc:interceptors>

In the above configuration you have to take care of below points

a) basenamePrefix  property of ResourceBundleThemeSource , tells about the prefix of properties file name, it means if it is "theme-", then the properties file name will start like " theme-*.properties ". Here I used the * ,because this place will be fill by the  property " paramName " of  " ThemeChangeInterceptor "

b) paramName property of ThemeChangeInterceptor tells about the parameter name , through which you have to pass the css file name dynamically.

c) defaultThemeName property of CookieThemeResolver, tells about the default css file name.

Now from the above information given by user springframework decide the runtime css needed for your application. For an example, if user hit the below request to the web application , then runtime properties for your application will be " theme-black.properties ", and this properties will reveal the location of your css file.

Request URL : http://localhost:8080/springTheme/?theme=black

2) File needed to include in the classpath of your application
theme-black.properties
theme-default.properties

content of file(theme-black.properties) will be like below
style=css/black.css

same for the others

3) Create the css directory under WebContent directory

Configuration of JSP file

you have to entered below code snippet under the <head> tag

<link rel=" stylesheet " href= " <spring:theme code="<key_of_properties>"/>" type="text/css"/>

Here according to our example
key_of_properties value will be style

After configure your application by following above steps , you can start to use the Spring-Theme features


I am very thankful for  you guys , for viewing my first post " Java 1,7 Features ". And I hope in near future , I will help you guys from my next blog on the SessionThemeResolver .