Spring Boot application enables an ability of switching themes.
- Import dependencies
<dependency>
<groupId>com.yookue.springstarter</groupId>
<artifactId>theme-change-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
By default, this starter will auto take effect, you can turn it off by
spring.theme-change.enabled = false
- Configure Spring Boot
application.yml
with prefixspring.theme-change
spring:
theme-change:
model-and-view:
view-attribute: 'themeChangeTagNames'
tag-multilingual: false
tag-names:
'light': 'Light'
'blue': 'Blue'
'dark': 'Dark'
theme-interceptor:
param-name: 'theme'
intercept-paths:
- '/**'
exclude-paths:
- '/foo/**'
- '/bar/**'
theme-source:
basename-prefix: 'theme.'
theme-resolver-type: 'cookie'
cookie-theme-resolver:
default-theme-name: 'light'
- Create basename folder and
properties
files. Thebasename-prefix
path is used forMessageSource
to resolvetheme
parameter (don't forgot the ending dot). You should- Create a folder named
theme
under the classpath (e.g. under theresources
folder). - Create several
properties
files named the keys oftag-names
node (e.g.light.properties
,blue.properties
,dark.properties
) under thetheme
folder. - Write a key with a path of style file in each
properties
file (takelight
theme as an example)/resource/theme/light.properties
content:
- Create a folder named
theme-change.style-sheet = /assert/theme/light.min.css
-
Optional feature: If you want to display the
tag-names
value in multilingual (not the fixed value inapplication.properties
), for example, display the value oflight
to明亮
when current request is usingzh
locale, or displayLight
when the request is usingen
locale. You can reach your goal as following:- Configure the
tag-multilingual
attribute totrue
- Set the value of
default
totheme-change.dropdown-switch
- Write the
theme-change.dropdown-switch
as a key in a resource bundle properties that could be loaded by the primaryMessageSource
bean (Just as our another Spring startermessage-source-spring-boot-starter
does)
- Configure the
-
Write your template, code as following (take
Thymeleaf
as an example)
Under the head
segment:
<link rel="stylesheet" type="text/css" th:href="@{${#themes.code('theme-change.style-sheet')}}"/>
Under the body
segment:
<ul>
<li class="theme-switch dropdown">
<a href="javascript:">
<i class="fa fa-futbol-o" aria-hidden="true"></i>
<span>Theme</span>
<i class="bi bi-chevron-down"></i>
</a>
<ul role="menu" th:if="${not #maps.isEmpty(themeChangeTagNames)}">
<li th:each="tagName: ${themeChangeTagNames}"><a th:href="${'?theme=' + tagName.key}"><span th:text="${tagName.value}">Placeholder</span></a></li>
</ul>
</li>
</ul>
- Github: https://github.com/yookue/theme-change-spring-boot-starter
- Spring MVC themes tutorial: https://www.baeldung.com/spring-mvc-themes
- jdk 1.8+
This project is under the Apache License 2.0
See the NOTICE.txt
file for required notices and attributions.
You like this package? Then donate to Yookue to support the development.
- Yookue: https://yookue.com