The following example shows how to customize the theme name. We'll still use FixedThemeResolver (used by default), but with a different theme name.
Note: As of Spring Framework 6.0, theme support (ThemeResolver, ThemeSource, FixedThemeResolver, and related classes) is deprecated in favor of using CSS directly, with no direct replacement planned. This tutorial remains useful for understanding how theme resolution worked in earlier versions of Spring MVC, but it should not be used as a basis for new applications.
Example
Java Config Class
DispatcherServlet looks for a user-configured ThemeResolver bean under a specific name, as defined by DispatcherServlet.THEME_RESOLVER_BEAN_NAME, before falling back to the default one:
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {
@Bean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME)
public ThemeResolver customThemeResolver() {
FixedThemeResolver fixedThemeResolver = new FixedThemeResolver();
fixedThemeResolver.setDefaultThemeName("app-theme");
return fixedThemeResolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//mapping '/' to index view name without a controller
ViewControllerRegistration r = registry.addViewController("/");
r.setViewName("index");
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp();//default prefix=/WEB-INF/", suffix=".jsp"
}
}
Theme Properties File
src/main/resources/app-theme.propertiesbackground=beige
content-style=width:500px;border:solid 2px blue;margin:auto;padding:30px;
JSP View
/src/main/webapp/WEB-INF/index.jsp<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<body style="background-color:<spring:theme code='background'/>;">
<div style="<spring:theme code='content-style'/>">
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.
Donec eu libero sit amet quam egestas semper. Aenean ultricies
mi vitae est. Mauris placerat eleifend leo.
</div>
</body>
</html>
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Output
$ curl -s "http://localhost:8080/"
<html> <body style="background-color:beige;"> <div style="width:500px;border:solid 2px blue;margin:auto;padding:30px;"> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. </div> </body> </html>
Example ProjectDependencies and Technologies Used: - spring-webmvc 6.0.0 (Spring Web MVC)
Version Compatibility: 4.1.0.RELEASE - 6.2.19 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.0.0 (Jakarta Servlet API documentation)
- JDK 17
- Maven 3.9.11
|