springboot集成thymeleaf(不重启刷新html)

springboot集成thymeleaf完整配置
1、pom文件新增spring-boot-starter-thymeleaf依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、application.properties新增thymeleaf配置

#######thymeleaf#########
# 模板配置
# 这个开发配置为false,避免改了模板还要重启服务器
spring.thymeleaf.cache=false
# 这个是配置模板路径的,默认就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/
# 这个可以不配置,检查模板位置
spring.thymeleaf.check-template-location=true
# 下面3个不做解释了,可以不配置
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html

# 模板的模式
spring.thymeleaf.mode=HTML5
#######thymeleaf#########

3、新建controller

@RequestMapping("/test")
    public String test(){
        List<TBookinfo> records = testService.getRecords();
        records.stream().forEach(r -> System.out.println(r.getAuthor()));
        return "index";
    }

4、在resource/template下新建index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hddyddytdy  sdfdfsdfs  sssss
</body>
</html>

这样就已经配置好了。但这时仍然有个问题:修改html文件需要重启才能生效,这样大大降低了开发效率。经过研究进行如下设置后即可无需重启服务器让文件生效

1、在application.propertis中新增如下配置

# 这个开发配置为false,避免改了模板还要重启服务器
spring.thymeleaf.cache=false

2、在工程中引入Spring Boot Dev Tools

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

3、在idea中intellij idea-perference-Build,Execution,Deployment-Complier中勾上Build project automatically
这里写图片描述
4、在idea中按command+option+shift+/选择Registry,勾选compiler.automake.allow.when.app.running
这里写图片描述

经过以上4个步骤即可实现不重启服务更新页面

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值