Thymeleaf
1.什么是Thymeleaf
Thymeleaf其实说白了就是jsp一样的东西,用来将html翻译成动态的一个东西,类似的还有freemark等等,因为jsp太老了几乎要被淘汰了。

注意这里中间需要一个template引擎也就是用来翻译的,thymeleaf只是一个模板罢了
2.如何将Thymeleaf引入到springboot项目中
Thymeleaf 官网:https://www.thymeleaf.org/
Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf
Spring官方文档:找到我们对应的版本
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
在上面这个连接中找到下面这个东西点击pom

找到对应的pom依赖:可以适当点进源码看下本来的包!
![]()
引入其中这两个依赖(这里注意要用spring5和jdk8)
Maven会自动下载jar包,我们可以去看下下载的东西;

打开ThymeleafProperties

创建如下东西:

我们可以去官方文档的#3中看一下命名空间拿来过来(其实这一步就像引入el标签库一样):
xmlns:th="http://www.thymeleaf.org"

看到没有这里就可以直接用el表达式来取了
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>狂神说</title> </head> <body> <h1>测试页面</h1> <!--th:text就是将div中的内容设置为它指定的值,和之前学习的Vue一样--> <!--所有的html元素都可以被thymeleaf替换接管: th:元素名--> <div th:text="${msg}"></div> </body> </html>

4.3常用语法

练习一:
controller:

html:

显示:

练习二:
controller:

html:

显示:

上面这种遍历也可以写成这个样子:

4.4我们能写哪些表达式呢?
Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:#18
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.
3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
==================================================================================
Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
Message Expressions: #{...}:获取国际化内容
Link URL Expressions:


