IntelliJ IDEA SpringBoot Thymeleaf 热部署,实现修改java文件和html文件无需手动编译重启即可

Thymeleaf配置与Spring Boot热部署
本文介绍如何配置Thymeleaf模板引擎,包括禁用缓存、设置前后缀、模式和编码,同时引入Spring Boot DevTools依赖实现应用的自动重启,最后开启IDEA的自动构建功能。

1:thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.devtools.restart.enabled=true

2.在 pom.xml 里添加spring-boot-devtools 依赖

org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin true

3.开启 idea 自动 build 功能
在这里插入图片描述
ctrl+shift+a 搜索registry

在这里插入图片描述
在这里插入图片描述

### Thymeleaf 热部署配置教程 在使用 Spring Boot DevTools 实现 Thymeleaf 模板的热部署时,可以通过以下方式完成配置: #### 1. 引入 Spring Boot DevTools 为了启用热部署功能,首先需要引入 `spring-boot-devtools` 依赖项。这一步通常通过项目的构建工具(Maven 或 Gradle)来完成。 对于 Maven 构建项目,可以在 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> ``` 对于 Gradle 构建项目,则需在 `build.gradle` 中加入以下内容: ```gradle dependencies { developmentOnly('org.springframework.boot:spring-boot-devtools') } ``` 此操作确保了开发环境下的热部署功能得以激活[^1]。 #### 2. 配置 application.yml 或 application.properties 为了让 Thymeleaf 支持模板文件的实时更新,在配置文件中关闭其缓存机制是非常重要的。具体设置如下所示: 如果采用的是 YAML 格式的配置文件 (`application.yml`) ,则应写成这样: ```yaml spring: devtools: restart: enabled: true # 启用自动重启功能 additional-paths: src/main/resources/templates # 添加监控路径 exclude: static/,templates/WEB-INF/** # 排除不需要监听变化的资源目录 thymeleaf: cache: false # 关闭 Thymeleaf 缓存以便即时反映改动 ``` 如果是基于 `.properties` 的配置形式,则对应的条目为: ```properties spring.devtools.restart.enabled=true spring.devtools.restart.additional-paths=src/main/resources/templates spring.devtools.restart.exclude=static/,templates/WEB-INF/** spring.thymeleaf.cache=false ``` 这些参数的作用在于使每次保存 HTML 文件或者控制器逻辑之后,浏览器端能够立刻展示最新的界面效果而无需重新启动整个应用程序[^2]。 #### 3. IDEA 特殊配置 (可选) 某些情况下,默认集成可能无法完全满足需求,特别是当 IDE 设置不当时可能会阻碍正常工作流。针对 IntelliJ IDEA 用户来说,还需要做一些额外调整才能让一切顺利运作起来: - **禁用 Power Save Mode**: 这种模式下会暂停后台编译器活动从而影响到快速反馈循环。 - **开启 Compiler Auto Make**: 前往 File -> Settings -> Build, Execution, Deployment -> Compiler 并勾选 “Build project automatically”。 - **注册快捷键 Ctrl+F9 自动触发增量编译** : 如果发现修改代码后仍看不到预期行为的话,尝试绑定这个命令至常用按键组合方便随时执行局部重建动作。 以上步骤完成后应该就可以享受到无缝衔接般的高效调试体验啦! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值