SpringBoot:Thymeleaf模板引擎

本文介绍了在SpringBoot中使用Thymeleaf作为模板引擎的原因和步骤。由于SpringBoot不支持jsp,Thymeleaf成为了一个理想的选择。内容包括如何引入Thymeleaf的依赖,理解其自动配置规则,以及设置html页面存放位置以实现自动渲染。通过一个简单的demo,展示了在controller中如何处理test.html,并建议读者参考官方文档或视频深入学习Thymeleaf的语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端提交的往往只是html页面,在此基础上需要我们转为一些模板引擎来进行操作,在这之前熟悉的jsp就是一个模板引擎,同时它也是个servlet。但是springBoot不支持jsp模板引擎。所以对于Thymeleaf的学习就很有必要。是一个高级语言的模板引擎,他的这个语法更简单。

1、引入Thymeleaf
在SpringBoot中都是start一下就够了,其中需要去Thymeleaf官网:https://www.thymeleaf.org/

找到对应的pom依赖:


<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Thymeleaf模板引擎分析:
首先得按照SpringBoot的自动装配原理看一下该Thymeleaf的自动装配规则。

Thymeleaf的自动配置类:ThymeleafProperties

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
}

html页面放在类路径下的templates下,thymeleaf就可以实现自动渲染了。

可以写一个简单的demo演示,controller test.html等等 记住test.html放在templates目录下。

至于 thymeleaf 的语法学习可以参考官方文档或者视频解读。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值