Themes and Theme resolvers in Spring MVC

In this section, you will learn about theme and theme resolvers in Spring MVC.

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

Themes and Theme resolvers in Spring MVC

In this section, you will learn about theme and theme resolvers in Spring MVC.

Themes in a application can be define as overall look-and-feel. Basically, theme is a collection of static resources like images, CSS etc.

Defining themes

For using theme in your application, you must use interface org.springframework.ui.context.ThemeSource. ThemeSource is extended by the WebApplicationContext interface but real work is done by the implementation of org.springframework.ui.context.support.ResourceBundleThemeSource that loads properties files from the root of the classpath.

You can register a bean in the application context with the reserved name themeSource for custom ThemeSource implementation or to configure the base name prefix of the ResourceBundleThemeSource. A bean with that name is automatically detected and use by the web application context.

Using ResourceBundleThemeSource, you can define a theme in properties file. You need to make a list of resources inside property file. Given below a sample :

styleSheet=/themes/cool/style.css
background=/themes/cool/img/coolBg.jpg

The keys of the property file represents the themed element of view. For example : in JSP, you can use <spring:theme> custom tag to refer a themed elements. Given below the sample code :

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
</head>
<body style="background=<spring:theme code='background'/>">
...
</body>
</html>

The properties files are loaded from the root of the classpath.

Theme resolvers

After defining theme, you decide which theme to use. The DispatcherServlet look for a bean named themeResolver to determine which implementation of ThemeResolver to use. It detects the theme for a specific request and can also modify the theme of the request. Spring have following theme resolvers:

Class Description
FixedThemeResolver This theme resolver picks fixed theme which can be set using defaultThemeName property.
SessionThemeResolver This theme resolver is used to set the theme for a whole session but not for different session.
CookieThemeResolver This theme resolver set the selected theme in a cookie for each client.
 ThemeChangeInterceptor This theme resolver changes theme on every request having a simple request parameter.

Advertisements

Share on Google+Share on Google+

Themes and Theme resolvers in Spring MVC

Posted on: January 11, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Themes and Theme resolvers in Spring MVC  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads