网上一些博客关于idea热部署真是误导人,对于springboot的thymeleaf页面更改不重启的博客更是五花八门,下面这篇转载的博客本人亲测有效,遗憾的是没有找到原文地址.....
这里简单记录一下Thymeleaf配置和使用的步骤
1、修改pom文件,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.6.2</version>
</dependency>
说明:使用springboot的thymeleaf模板时默认会对HTML进行严格的检查,导致当你的标签没有闭合时就会通不过。nekohtml这个依赖可以解决这一问题。
2、简单配置一下:
spring.thymeleaf.cache=false
spring.thymeleaf.mode = LEGACYHTML5
说明:
第一行配置是清除缓存,实现热部署。也就是修改了html后不用重启,刷新页面就能看到效果。不过这儿特别强调一下,修改完html后一定要ctrl+f9重新build一下。再回到浏览器刷新,就能看到效果了,就这个花了我一个小时去找答案。
第二行配置是回避HTML进行严格的检查的配置,当然你需要提前引入nekohtml依赖。
注意:引入
Thymeleaf
依赖后,可以不用在application.properties里配置,默认会在resource.templates里找 html的页面。
================================================================================================
有了对于Thymeleaf的热部署,那么对于java代码的热部署也不能少。
1. 引入热部署jar包(本人springboot使用的是2.1.4版本)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
2. 配置application.yml文件开启热部署
spring:
devtools:
restart:
enabled: true # 配置热部署
additional-paths: src/main/java
exclude: WEB-INF/**
3. 修改IDEA设置
File-Settings-Compiler-Build Project automatically
4. ctrl + shift + alt + /然后选择Registry,勾上 Compiler.autoMake.allow.when.app.running
这样SpringBoot的Thymeleaf和Java代码的热部署就可以使用了!