Thymeleaf模板引擎
模板引擎
前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。
jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。
针对SpringBoot可以使用模板引擎
模板引擎就是我们写一个页面模板,我们在后台封装一些数据,然后将数据和模板交给模板引擎,模板引擎按照数据将页面中的一些表达式进行解析,并填充到指定位置,最后把这个数据生成想要的内容写出去。
引入Thymeleaf
都是基于3.xx开发
对于SpringBoot引入
Thymeleaf 官网:https://www.thymeleaf.org/
Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
模板引擎使用
只要使用Thymeleaf,只需要导入对应的依赖,导入之后将html页面放在templates目录下
Thymeleaf所有的模板引擎都写在template路径下
使用方法:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
在htnl中导入
<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html>
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" data-th-href="@{/css/gtvg.css}" />
</head>
<body>
<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
</body>
</html>