LocaleChangeInterceptor
to intercept the change in locale and ReloadableResourceBundleMessageSource
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. [sc:SpringMVC_Tutorials]
<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>
Code language: HTML, XML (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 class org.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-blue.propertiesCode language: HTML, XML (xml)css=themes/default.css
File: resources/theme-black.propertiesCode language: HTML, XML (xml)css=themes/blue.css
Code language: HTML, XML (xml)css=themes/black.css
body {
background-color: white;
color: black;
}
Code language: CSS (css)
File: WebContent/themes/blue.css body {
background-color: #DBF5FF;
color: #007AAB;
}
Code language: CSS (css)
File: WebContent/themes/black.css body {
background-color: #888;
color: white;
}
Code language: CSS (css)
<%@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>
Code language: HTML, XML (xml)
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. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
That worked like Charm. Thank you for all the great work.
Naveen
Aren't you missing the link to the stylesheet.
<link rel="stylesheet" href='' type="text/css" />
I had to stick this within the HEAD tag in layout.jsp for the themes to work.
You have not mentioned the changes required in layout.jsp to support themes.
thanks for the post
Hi thanks for the tutorial! Do you have example of using ajax with Spring MVC 3?
This doesnt seem to work as is. Any changes required in layout.jsp?
Please check the attached code sample. layout.jsp page need <link rel="stylesheet" href="" type="text/css"/> line.
Can this feature be implemented using Spring 2.5? If so, please let me know the changes. Thanks in advance.
Please add in layout.jsp the following codes:
<link rel="stylesheet" href="" type="text/css"/>
or you may refer to the attached codes.
Thank you very much. Very comprehensive tutorial.
@Nasa - Thanks for the correction.
Estan todos muy tlacucahes aqui esta la correcion :)
en el jsp template
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<link rel="stylesheet" href="<spring:theme code="css"/>" type="text/css"/>
J_J gracias al compa oswaldo