• SpringBoot集成Thymeleaf——关闭页面缓存——设置热部署


    SpringBoot集成Thymeleaf——关闭页面缓存——设置热部署

    SpringBoot 静态资源 - 模板引擎Thymeleaf

    1. 引入依赖

    SpringBoot框架集成Thymeleaf的起步依赖

    <!--SpringBoot框架集成Thymeleaf的起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    2. 关闭模板引擎缓存

    在核心配置文件中设置src/main/resources/application.properties

    #关闭Thymeleaf模板引擎缓存,,,默认是开启的
    spring.thymeleaf.cache=false
    
    
    • 1
    • 2
    • 3

    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";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5. 热部署设置

    切出编辑器会自动更新启动:不需要重新启动程序,再次访问可以访问到页面更新后的效果
    在这里插入图片描述

    6. 测试

    6.1 直接访问结果截图

    在这里插入图片描述

    6.2 修改页面再次访问结果截图

    在页面上添加一个

    修改内容,再次访问

    ,,在 不重启项目的前提下再次访问

    在这里插入图片描述

    访问结果如下:

    在这里插入图片描述

    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>
    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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}">
    
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    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}">
        
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    【夜读】提升生活品质的8个建议
    linux系统宝塔服务器temp文件夹里总是被上传病毒php脚本
    Linux进程间通信
    Thread类的基本用法
    数据结构-堆和二叉树
    后仿真中的 《specify/endspecify block》之(5)使用specify进行时序仿真
    第4章 抽象:进程
    魅族回应被吉利收购:已签署协议;腾讯下架QQ影音所有版本;PyPI多个软件包因拼写错误包含后门|极客头条
    协程的介绍
    .Net6新版本的AssemblyLoadContext 加载程序集和卸载程序集
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/126513347