Thymeleaf模板引擎
SpringBoot集成Thymeleaf——关闭页面缓存——设置热部署
SpringBoot 静态资源 - 模板引擎Thymeleaf
1. 引入依赖
SpringBoot框架集成Thymeleaf的起步依赖
<!--SpringBoot框架集成Thymeleaf的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guo</groupId>
<artifactId>springboot-Thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot框架集成Thymeleaf的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--SpringBoot项目编译打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. 关闭模板引擎缓存
在核心配置文件中设置src/main/resources/application.properties
#关闭Thymeleaf模板引擎缓存,,,默认是开启的
spring.thymeleaf.cache=false
3. 控制层
src/main/java/com/guo/springboot/web/StudentController.java
package com.guo.springboot.web;
import com.guo.springboot.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class StudentController {
@RequestMapping("/user/detail")
public String detail(Model model){
Student student = new Student();
student.setId(1001);
student.setName("张三");
model.addAttribute("student",student);
return "detail";
}
}
4. 创建HTML页面添加命名空间
标准表达式${}
路径表达式@{}
添加命名空间
xmlns:th="http://www.thymeleaf.org"
src/main/resources/templates/detail.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Thymeleaf模板引擎</h1>
<div>
用户ID:<span th:text="${student.id}"></span> <br/>
用户名称:<span th:text="${student.name}"></span><br/>
</div>
</body>
</html>
5. 热部署设置
切出编辑器会自动更新启动:不需要重新启动程序,再次访问可以访问到页面更新后的效果
6. 测试
6.1 直接访问结果截图
6.2 修改页面再次访问结果截图
在页面上添加一个
<h2>修改内容,再次访问</h2>
,,在 不重启项目的前提下再次访问
访问结果如下:
7. Thymeleaf遍历集合
7.1 循环遍历List集合
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>循环遍历list集合</title>
</head>
<body>
<!--
student 当前循环的对象变量名称
studentStat 当前循环对象状态的变量(可写可不写)
${studentList} 当前循环的集合
-->
<div th:each="student,studentStat:${studentList}">
<!--<div th:each="student:${studentList}">-->
<span th:text="${studentStat.index}"></span>
<span th:text="${student.id}"></span>
<span th:text="${student.name}"></span>
<span th:text="${student.address}"></span>
</div>
</body>
</html>
7.2 循环遍历Map集合
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>循环遍历Map集合</title>
</head>
<body>
<div th:each="studentMap,studentMapStat:${studentMaps}">
<!--<div th:each="studentMap:${studentMaps}">-->
<span th:text="${studentMap.key}"></span>
<span th:text="${studentMap.value}"></span>
<span th:text="${studentMap.value.id}"></span>
<span th:text="${studentMap.value.name}"></span>
<span th:text="${studentMap.value.address}"></span>
</div>
</body>
</html>
7.3 循环遍历Array数组
循环遍历Array数组和循环遍历List方法一样
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>循环遍历Array数组</title>
</head>
<body>
<div th:each="student,studentStat:${studentArray}">
<!--<div th:each="student:${studentList}">-->
<span th:text="${studentStat.index}"></span>
<span th:text="${student.id}"></span>
<span th:text="${student.name}"></span>
<span th:text="${student.address}"></span>
</div>
</body>
</html>