SpringBoot集成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>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

848698119

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值