Thymeleaf Multiple Template Locations using Spring Boot
1. Overview
In this tutorial, we’ll see how we can define multiple template locations using Thymeleaf in a Spring Boot application.
2. Maven Dependencies
Firstly, we’ll add the spring-boot-starter-web and spring-boot-starter-thymeleaf Maven dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
3. Default Configuration
By default, Thymeleaf will look for the templates in the templates/ directory on the classpath.
Though we can configure this location with the spring.thymeleaf.prefix property in application.properties:
spring.thymeleaf.prefix=classpath:/templates/
Now, we’ll create a controller to investigate the template path resolution in detail.
@Controller
public class TemplateLocationController {
@RequestMapping("/welcome")
public String sayWelcome() {
return "welcome";
}
}
Here, we have the TemplateLocationController class which has a single endpoint. Since this endpoint returns welcome as the template name and the prefix is classpath:/templates/, the final path becomes classpath:/templates/welcome.html. During the development time, this template resides at src/main/resources/templates/welcome.html – if we use the default Maven folder structure.
- Defining Multiple Locations
To define multiple template locations, we must define several Spring beans implementing the ITemplateResolver interface. Thymeleaf provides several implementation classes of ITemplateResolver like SpringResourceTemplateResolver and ClassLoaderTemplateResolver:
@Configuration
public class TemplateResolverConfiguration {
@Bean
public SpringResourceTemplateResolver firstTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/templates/templatelocation/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(0);
templateResolver.setCheckExistence(true);
return templateResolver;
}
@Bean
public ClassLoaderTemplateResolver secondTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/templatelocation/other/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(1);
templateResolver.setCheckExistence(true);
return templateResolver;
}
@Bean
public ClassLoaderTemplateResolver thirdTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/templatelocation/another/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(2);
templateResolver.setCheckExistence(true);
return templateResolver;
}
}
Here, we’re creating one SpringResourceTemplateResolver and two ClassLoaderTemplateResolver beans. During the initialization, we’re assigning different paths using the setPrefix method. Additionally, we’re defining the order of the beans using the setOrder method.
As a result, when a controller method returns a view name, Thymeleaf will look for it in four different locations respectively: /templates/templatelocation/, /templates/templatelocation/other/, /templates/templatelocation/another/ and/templates/.
5. Summary
In this tutorial, we’ve looked at how we can define multiple template locations using Thymeleaf in a Spring Boot application.
Finally, check out the source code for all examples over on Github.
Thymeleaf Multiple Template Locations using Spring Boot的更多相关文章
- 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】
[原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...
- spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)
一,什么情况下会用到嵌套循环? 当我们展示多个分类时,每个分类下又展示出推荐的前几个商品, 这时我们需要用到嵌套循环 看一个例子: 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:/ ...
- spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)
这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 <dependency> <groupId>org.springframework.boot& ...
- Spring boot 整合 Mybatis + Thymeleaf开发web(二)
上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...
- Spring Boot (四)模板引擎Thymeleaf集成
一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...
- (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...
- Spring Boot集成Thymeleaf
Thymeleaf是一个java类库,是一个xml/xhtml/html5的模板引擎,可以作为mvc的web应用的view层.Thymeleaf提供了额外的模块与Spring MVC集成,所以我们可以 ...
- Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越 ...
- Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图
静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...
随机推荐
- mintUI和mUI
mintUI 安装: npm install mint-ui -S 引入: // 按需引入部分组件 import { Cell, Checklist } from 'mint-ui'; Vue.com ...
- Java学习笔记【一、环境搭建】
今天把java的学习重新拾起来,一方面是因为公司的项目需要用到大数据方面的东西,需要用java做语言 另一方面是原先使用的C#公司也在慢慢替换为java,为了以后路宽一些吧,技多不压身 此次的学习目标 ...
- 一篇文章让你彻底明白__getattr__、__getattribute__、__getitem__的用法与执行原理
__getattr__ 在Python中,当我们试图访问一个不存在的属性的时候,会报出一个AttributeError.但是如何才能避免这一点呢?于是__getattr__便闪亮登场了 当访问一个不存 ...
- Java语言基础(9)
1 方法(二) 1) 不带参数没有返回值的方法: 案例:Demo1 public class Demo1 { static void show(){ System.out.println(" ...
- The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)
In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...
- Codeforce Round #424
A 略 B 略 C: 先对Ai数列预处理前缀和 然后把Bi的每个都加一次 最终得到的结果为ans[sum]++; 最后如果有一个ans[sum]>=k即满足所有K个条件就输出(注意!!前缀和要进 ...
- puppet完全攻略(二)让puppet代码支持vim高亮显示
puppet完全攻略(二)让puppet代码支持vim高亮显示 2012-06-10 13:33:01 标签:puppet viong puppet完全攻略 原创作品,允许转载,转载时请务必以超链接形 ...
- 第十一章 前端开发-css
第十一章 前端开发-css 1.1.0 css介绍 css是指层叠样式表(Cascading Style Sheets),样式定义如何显示html元素,样式通常又会存在于样式表中. css优势: 内容 ...
- Luogu P4331 [BOI2004]Sequence 数字序列 (左偏树论文题)
清晰明了%%% Fairycastle的博客 个人习惯把size什么的存在左偏树结点内,这样在外面好写,在里面就是模板(只用修改update). 可以对比一下代码(好像也差不多-) MY CODE # ...
- Acwing-196-质数距离(素数区间筛法)
链接: https://www.acwing.com/problem/content/198/ 题意: 给定两个整数L和U,你需要在闭区间[L,U]内找到距离最接近的两个相邻质数C1和C2(即C2-C ...