Spring Boot中使用模板引擎Thymeleaf

Posted edda_huang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot中使用模板引擎Thymeleaf相关的知识,希望对你有一定的参考价值。

一、Thymeleaf简介

Thymeleaf[ta?m lif],百里香叶,是一个流行的模板引擎,该模板引擎采用Java语言开发。Java中常见的模板引擎有Velocity、Freemaker、Thymeleaf等。不同的模板引擎都会具有自己的特定的标签体系,而Thymeleaf以html标签为载体,在HTML的标签下实现对数据的展示。
          Thymeleaf本身与SpringBoot是没有关系的,但SpringBoot官方推荐使用Thymeleaf作为前端页面的数据展示技术,SpringBoot很好地集成了这种模板技术。
Thymeleaf的官网为:https://www.thymeleaf.org/

二、创建工程

创建一个Spring Boot工程,命名为08-thymeleaf,并在创建工程时导入如下依赖。

技术图片

三、定义配置文件

技术图片

四、定义处理器

技术图片

五、定义index.html页面

在src/main/resources/templates目录下定义index.html页面

技术图片

在页面的<html>标签中需要添加Thymeleaf的命名空间属性:xmlns:th="http://www.thymeleaf.org"

技术图片

五、测试

http://localhost:8080/test/index

技术图片

 



以上是关于Spring Boot中使用模板引擎Thymeleaf的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot中使用模板引擎Thymeleaf

Spring Boot Thymeleaf 模板引擎的使用

Spring Boot 2.0 使用模板引擎返回JSP页面实战

springboot中模板 freemark,thymeleaf,jsp

Spring Boot 最佳实践模板引擎Thymeleaf集成

Spring Boot 最佳实践模板引擎Thymeleaf集成