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.

  1. 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的更多相关文章

  1. 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...

  2. spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)

    一,什么情况下会用到嵌套循环? 当我们展示多个分类时,每个分类下又展示出推荐的前几个商品,   这时我们需要用到嵌套循环 看一个例子: 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:/ ...

  3. spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)

    这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 <dependency> <groupId>org.springframework.boot& ...

  4. Spring boot 整合 Mybatis + Thymeleaf开发web(二)

    上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...

  5. Spring Boot (四)模板引擎Thymeleaf集成

    一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...

  6. (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...

  7. Spring Boot集成Thymeleaf

    Thymeleaf是一个java类库,是一个xml/xhtml/html5的模板引擎,可以作为mvc的web应用的view层.Thymeleaf提供了额外的模块与Spring MVC集成,所以我们可以 ...

  8. Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越 ...

  9. Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图

    静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...

随机推荐

  1. vue中使用qrcode,遇到两次渲染的问题

    1.安装 qrcodejs2: npm install qrcodejs2 --save 2.页面中引入: import QRCode from "qrcodejs2";   co ...

  2. 判断一个数是否为回文数(js)

    //判断是否为回文数:若n=1234321,则称n为一回文数 let readline = require("readline-sync"); let newNum = 0; co ...

  3. 云端js动态效果

    效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  4. asp.net Core 2.0 MVC为Controller或Action添加定制特性实现登录验证

    前言:最近在倒腾 微软的新平台 asp.net Core 2.0,在这个过程中有些东西还是存在差异.下面是我在学习过程的一点笔记.有不妥之处,望各位大虾指正! 一.先创建一个控制器继承于Control ...

  5. ubuntu根目录下空间不足,syslog占用很大空间,如何清理?

    一激动差点儿删除,以下清理方式是对的 cat /dev/null > /var/log/syslog

  6. 能当壁纸用的Git常用命令速查表

    使用Microsoft Office 2016手工绘制. 链接: https://pan.baidu.com/s/18KsH-u5T2iSTHaXd6iQWGA 提取码: w8da 复制这段内容后打开 ...

  7. python之判断和循环

    计算机之所以能做很多自动化的任务,因为它可以自己做条件判断.比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = : print ('your age i ...

  8. Linux根文件系统和目录结构及bash特性2

    Linux系统上的文件类型:    -:常规文件,即f    d:directory,目录文件    b:block device,块设备文件,支持以“block”为单位进行随机访问    c:cha ...

  9. Python中关于txt的简单读写模式与操作

    Python中关于txt的简单读写操作 常用的集中读写模式: 1.r 打开只读文件,该文件必须存在. 2.r+ 打开可读写的文件,该文件必须存在. 3.w 打开只写文件,若文件存在则文件长度清为0,即 ...

  10. [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]

    [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...