1、pom.xml 注入模板引擎thymeleaf依赖,切换thymeleaf版本为3.0
代码语言:javascript复制<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序thymeleaf3主程序 layout2以上版本-->
<!-- thymeleaf2 layoout1-->
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、thymeleaf语法
代码语言:javascript复制@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
@RequestMapping("/success")
public String success(){
// classpath:/templates/success.html
return "success";
}
}
给sucess.html中设置内容
代码语言:javascript复制@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
@RequestMapping("/success")
public String success(Map<String,Object> map){
// classpath:/templates/success.html
map.put("hello","你好");
return "success";
}
}
代码语言:javascript复制<!DOCTYPE html>
<!--导入thymeleaf语法提示-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success!!!!!!!</h1>
<!--th:text 改变当前元素里面的文本内容-->
<!--th:任意html属性;来替换原生属性的值-->
<div th:text="${hello}"></div>
</body>
</html>
3、th的语法
代码语言:javascript复制th:insert 片段包含
th:replace 片段包含
th:each 遍历
th:if 判断
th:unless 条件判断
th:switch 条件判断
th:case 条件判断
th:object 声明变量
th:with 声明变量
th:attr 任意属性修改支持prepend,append
th:attrprepend 任意属性修改支持prepend,append
th:attrappend 任意属性修改支持prepend,append
th:value 修改指定属性默认值
th:href 修改指定属性默认值
th:src 修改指定属性默认值
th:text 修改标签体内容,转义特殊字符
th.utext 修改标签体内容,不转义特殊字符
th:fragment 声明片段
th:remove 声明片段