模板引擎thymeleaf
模板引擎的作用就是写一些页面的模板;
让controller可以跳转访问html页面,只需要引入模板引擎即可;
在pom.xml引入maven依赖;
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
在html文件中引入头文件约束:xmlns:th="www.thymeleaf.org
<html lang="en" xmlns:th="http://www.thymeleaf.org">
关闭模板引擎缓存
spring.thymeleaf.cache=false
thymeleaf语法
所有的html元素都可以被thymeleaf代替接管:th:元素
例如:
<div th:text="${msg}"></div>
${} 取普通变量的值
#{} 国际化信息
@{} 可取url的值
~{} 取片段表达式
所有的静态资源都需要使用thymeleaf接管:@{}