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.

  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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xidianzxm

您的打赏是我继续创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值