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

原文地址:https://www.cnblogs.com/edda/p/13261667.html