Spring Boot学习笔记(十四):Spring Boot 引入 Thymeleaf 及使用介绍

引言

        Spring Boot 推荐我们使用模板引擎 Thymeleaf 来开发页面,因为它语法简单,功能强大。作为模板引擎,Thymeleaf 和市面上主流其他的 Java 模板引擎:JSPVelocityFreemarker,原理都是类似的。

  1. 模板引擎的作用:将模板(我们开发的页面)和 数据进行整合,然后输出内容显示的过程。
  2. 模板引擎的区别:不同的模板都有它们自己不同的语法。

1.Spring Boot 引入 Thymeleaf

       Thymeleaf 官网:我是官网链接,Thymeleaf 已经将代码托管在了 Github 上:我是Github地址。Spring Boot 如何引入 Thymeleaf 模板,我们只需要在 pom.xml 中添加如下 Maven 依赖即可。

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

1.1 修改 Thymeleaf 版本

       因为 Spring Boot 的自动配置,它已经为我们 Spring Boot 的每个版本都指定了各个组件的版本。如果你还在使用 Spring Boot 1.x 版本,它为我们自动指定的Thymeleaf 版本为 2.x 版本。在项目开发中,2.x 的 Thymeleaf 版本有点低,建议您升级到 Thymeleaf 3.x 版本。Spring Boot 官方有介绍我们该如何使用 Thymeleaf 3.x 版本。官网地址。我们只需要修改 Maven 依赖的 Thymeleaf 版本即可。

<properties>
    <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
</properties>

1.2 修改 Thymeleaf Layout Dialect 版本

       修改 Thymeleaf 的同时,必须同时修改 Thymeleaf Layout Dialect 布局组件的版本。Thymeleaf Layout Dialect 布局组件从 2.0.0 版本开始支持 Thymeleaf 3 。 Thymeleaf Layout dialect 2.0.0 rewritten to support Thymeleaf 3 。官网有提及:我是官网说明。所以此处均使用目前最新版本,Thymeleaf:3.1.11.RELEASE,Thymeleaf Layout Dialect:2.4.1
在这里插入图片描述

2.Thymeleaf 语法介绍

发布了301 篇原创文章 · 获赞 66 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/lzb348110175/article/details/105174349
今日推荐