This example presents the basic concept of using in Spring the changing theme.
The technologies used are :
-
– Spring 4.3.18
– JDK 1.80
– Maven 3.3.9
You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :
mvn eclipse:eclipse
Inspired from « Beginning Spring » Mert Caliskan, Kenan Sevindik, Rod Johnson (Foreword by), Jürgen Höller (Foreword by).
package javaspringexamples.spring.changingtheme.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @author mounir.sahrani@gmail.com
*
*/
@Controller
public class welcomeController {
@RequestMapping(value = "/welcome")
public ModelAndView user() {
return new ModelAndView("welcome");
}
}
style=css/white.css
style=css/yellow.css
body {
background-color: white;
color: black;
}
body {
background-color: yellow;
color: green;
}
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.mvc</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan
base-package="javaspringexamples.spring.changingtheme" />
<context:annotation-config />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource" />
<bean id="themeResolver"
class="org.springframework.web.servlet.theme.SessionThemeResolver">
<property name="defaultThemeName" value="yellow" />
</bean>
<mvc:interceptors>
<bean id="themeChangeInterceptor"
class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
</mvc:interceptors>
</beans>
<%
response.sendRedirect("welcome.mvc");
%>
<%@ page contentType="text/html;charset=ISO-8859-9"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <html> <head> <title>JavaSpringExamples.io - Spring MVC Changing Theme</title> <link rel="stylesheet" href="<spring:theme code="style"/>" type="text/css" /> </head> <body> Theme : <a href="?theme=white">White theme</a> - <a href="?theme=yellow">Yellow theme</a> <br /> <h1>JavaSpringExamples.io - Changing theme</h1> </body> </html>
Get the sources of the example from the following GitHub url
