SpringBoot 整合 Thymeleaf|学习笔记

简介: 快速学习 SpringBoot 整合 Thymeleaf

开发者学堂课程【SpringBoot 实战教程:SpringBoot 整合 Thymeleaf】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/651/detail/10796


SpringBoot 整合 Thymeleaf


1、springboot 官方不推荐使用jsp页面,它推荐使用模版引擎,thymeleaf 是模版引擎中的一种,在 springboot 中如何整合 thymeleaf。

2、以下是 springboot 提供的相关依赖,把依赖放进工程中。

<dependency>

<groupId>org. springf ramework . boot </ groupId>

<arti factId>spring-boot-starter- thymeleaf</artifactId>

</dependency>

3、事先创建好空的工程,跟 web 进行整合,把依赖拷贝进去。它都是基于模版的,springboot 它默认去 resources 下 templates 文件夹下找相应的模版,所以需要创建文件夹。这是它默认找的,如果不希望用这个文件夹,可以改成别的名字。

4、在 templates 文件夹中创建一个 thymeleaf 模版,创建 html,命名为 tests.html。

5、关于 html 静态的东西都可以使用,让它显示一个动态的数据,比如h1,注意使用 thymeleaf 取值的方式,

< ! DOCTYPE html >

<html >

<head>

<meta charset= "UTF-8">

<title> Insert title here</title>

< /head>

<body>

<h1 th:text="${word} "></h1>

</body>

</html>

6、通过 controller 产生数据,在 controller 里写一个功能,指定访问路径,构造数据使用 model,导入,返回最终要运行模版,根据 model 数据,取到对应的单词,显示在模版上。

@Controller

public class

IndexController {

@RequestMapping ("/ thymeleafpage")

public String show (Model model)

{

model . addAttribute ("word", "单词") ;

Return "tests";

}

}

7、启动,访问路径 thymeleafpage,会看到有错误,这个错误不是找不到模版页面,而是500的错误。控制台上说明是解析的错误。

1668590076848.jpg

8、指明这是 html 格式,需要把模版相关的全局属性的配置拷贝到全局配置文件中。创建全局配置文件,命名为 application.properties。这是和 thymeleaf 模版相关的配置。

#springboot 整合 thymeleaf

#<!--关闭 thymeleaf 缓存开发时使用否则没有实时画面-->

spring. thymeleaf . cache= false  需要注意把 cache 缓存设置为 false

##检查模板是否存在,然后再呈现

spring . thyme leaf . check- template- location=true

#Content-Type 值

spring . thyme leaf . content- type=text/html指明了 html 的文本

#启用 MVC Thymeleaf 视图分辨率

spring . thymeleaf . enabled=true

##应该从解决方案中排除的视图名称的逗号分隔列表

spring . thyme leaf . excluded-view-names=

#模板编码

spring. thymeleaf .mode=LEGACYHTML5 指定使用的是 html5

#在构建 URL 时预先查看名称的前缀

spring . thyme leaf .prefix=classpath: /templates/  指定模版所在的位置

#构建 URL 时附加查看名称的后缀.

spring. thymeleaf .suffix= .html指定后缀

#链中模板解析器的顺序

#spring. thymeleaf. template-resolver-order= 0

#可以解析的视图名称的逗号分隔列表

#spring. thymeleaf. view-names=

#thymeleaf end

9、重新启动,又出现了另外的错误,500。

1668590091207.jpg

控制台提示是configurationexception配置错误,配置异常,现在指定的模版格式是html,LEGACYHTML5 属于非严格的html格式,需要依赖nekoHTML 1. 9.15 or newer的版本。maven 依赖如下:

<dependency>

<groupId>net. sourceforge . nekohtml </groupId>

<artifactId>nekohtml </artifactId>

<version>1.9.22</version>

</dependency>

加入到pom中,这是使用thymeleaf容易出现的一些问题,重新启动,访问刷新。这次取到值了。

1668590103124.jpg

10、注意要进行全局的配置,在全局配置里面指明它是 html 的,这时它是属于非严格的 html,还需要做另外的依赖,模版里面如何取值,这是 thymeleaf 的语法。

< ! DOCTYPE html >

<html >

<head>

<meta charset= "UTF-8">

<title> Insert title here</title>

< /head>

<body>

<h1 th:text="${word} "></h1>

</body>

</html>

相关文章
|
消息中间件 Java RocketMQ
Springboot 集成 Rocketmq 消费者|学习笔记
快速学习 Springboot 集成 Rocketmq 消费者
2246 1
Springboot 集成 Rocketmq 消费者|学习笔记
|
消息中间件 IDE Java
Springboot 集成 Rocketmq 生产者|学习笔记
快速学习 Springboot 集成 Rocketmq 生产者
1338 0
Springboot 集成 Rocketmq 生产者|学习笔记
|
NoSQL 安全 Java
SpringBoot 自动配置的原理|学习笔记
快速学习 SpringBoot 自动配置的原理
435 0
SpringBoot 自动配置的原理|学习笔记
|
Java 程序员 应用服务中间件
初识 SpringBoot|学习笔记
快速学习 初识 SpringBoot
305 0
|
SQL 监控 Java
SpringBoot的MyBatis工程配置|学习笔记
快速学习SpringBoot的MyBatis工程配置
404 0
SpringBoot的MyBatis工程配置|学习笔记
|
监控 Java 应用服务中间件
SpringBoot|学习笔记
快速学习SpringBoot
296 0
SpringBoot|学习笔记
|
存储 机器学习/深度学习 IDE
SpringBoot 项目与被开发快速迁移|学习笔记
快速学习 SpringBoot 项目与被开发快速迁移
SpringBoot 项目与被开发快速迁移|学习笔记
|
安全 Java 应用服务中间件
集中式整合之搭建Springboot环境|学习笔记
快速学习集中式整合之搭建Springboot环境
集中式整合之搭建Springboot环境|学习笔记
|
消息中间件 Java 测试技术
SpringBoot 集成 JUNIT 测试下单基本流程|学习笔记
快速学习 SpringBoot 集成 JUNIT 测试下单基本流程
540 0
SpringBoot 集成 JUNIT 测试下单基本流程|学习笔记
|
Java 开发者
SpringBoot 整合测试|学习笔记
快速学习 SpringBoot 整合测试
366 0
SpringBoot 整合测试|学习笔记