SpringBoot 静态资源 - 模板引擎Thymeleaf
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>
在核心配置文件中设置src/main/resources/application.properties
#关闭Thymeleaf模板引擎缓存,,,默认是开启的
spring.thymeleaf.cache=false
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";
}
}
标准表达式${}
路径表达式@{}
添加命名空间
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>
切出编辑器会自动更新启动:不需要重新启动程序,再次访问可以访问到页面更新后的效果
在页面上添加一个
,,在 不重启项目的前提下再次访问
修改内容,再次访问
访问结果如下:
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>循环遍历list集合title>
head>
<body>
<div th:each="student,studentStat:${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>
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}">
<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>
循环遍历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}">
<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>