Spring Boot自动配置为ThyemeLeaf注册的默认模板解析器是基于类路径的,这意味着它从编译的资源/ target / classes / **加载模板和其他静态资源。


要加载对资源(HTML,js,CSS等)的更改,我们可以

  • 每次都重新启动应用程序-这当然不是一个好主意!
  • 使用IntelliJ上的CTRL + F9或(如果您使用的是eclipse键映射,则使用CTRL + SHIFT + F9)或只是右键单击并单击编译来重新编译资源。
  • 或如下所述的更好的解决方案!

Thymeleaf包括一个基于文件系统的解析器,它直接从文件系统中加载模板,而无需通过类路径(已编译资源)。

请参见DefaultTemplateResolverConfiguration#defaultTemplateResolver中的代码段

 @Bean  public SpringResourceTemplateResolver defaultTemplateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setApplicationContext( this .applicationContext); resolver.setPrefix( this .properties.getPrefix()); 

属性前缀默认为“ classpath:/ template /”。 参见摘要ThymeleafProperties#DEFAULT_PREFIX

 public static final String DEFAULT_PREFIX = "classpath:/templates/" ; 

解决方案:

Spring Boot允许我们覆盖属性“ spring.thymeleaf.prefix”以指向源文件夹“ src / main / resources / templates /”,而不是默认的“ classpath:/ templates /”。

在application.yml | properties文件中:

 spring: thymeleaf: prefix: file:src/main/resources/templates/ #directly serve from src folder instead of target 

这将告诉运行时不要查看目标/文件夹。 而且您不需要每次在我们的src / main / resources / template上更新html模板时都重新启动服务器

那么JavaScript / CSS文件呢?

您可以进一步继续更新“ spring.resources.static-locations”以指向您的静态资源文件夹(保存js / css,图像等)

spring:resources:static-locations: file:src/main/resources/static/ #directly serve from src folder instead of target        cache:period: 0

完整代码:

好的做法是仅在开发过程中具有上述配置。 要具有生产系统的默认配置,可以使用“个人档案”并为每个环境定义单独的行为。

这是基于我们刚刚描述的完整代码段!

项目结构:

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>my-sample-app</artifactId><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>11</java.version></properties><dependencies><!-- the basic dependencies as described on the blog --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies><build><finalName>${build.profile}-${project.version}-app</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><profiles><!-- Two profiles --><profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation><properties><spring.profiles.active>dev</spring.profiles.active><build.profile>dev<build.profile></properties></profile><profile><id>prod</id><properties><spring.profiles.active>prod</spring.profiles.active><build.profile>prod<build.profile></properties></profile></profiles></project>

属性文件(yml)

application-dev.yml

 spring: profiles: active: dev thymeleaf: cache: false prefix: file:src/main/resources/templates/ #directly serve from src folder instead of target   resources: static -locations: file:src/main/resources/ static / #directly serve from src folder instead of target       cache: period: 0 

 

application-prod.yml(不会覆盖任何内容)

 spring: profiles: active: prod 

希望这可以帮助!

翻译自: https://www.javacodegeeks.com/2019/04/skip-cache-thyemeleaf-bypass-restarting-server.html

Spring Boot –如何跳过缓存thyemeleaf模板,js,css等以每次绕过重启服务器相关推荐

  1. Spring Boot 集成 Redis 实现缓存机制

    本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...

  2. 第 4-4 课:Spring Boot 中使⽤ Cache 缓存的使⽤

    我们知道绝⼤多数的⽹站/系统,最先遇到的⼀个性能瓶颈就是数据库,使⽤缓存做数据库的前置缓存,可以 ⾮常有效地降低数据库的压⼒,从⽽提升整个系统的响应效率和并发量. 以往使⽤缓存时,通常创建好缓存⼯具类 ...

  3. Spring Boot 整合Redis 实现缓存

    本文提纲 一.缓存的应用场景 二.更新缓存的策略 三.运行 springboot-mybatis-redis 工程案例 四.springboot-mybatis-redis 工程代码配置详解 运行环境 ...

  4. Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 一.缓 ...

  5. 注解参数获取不到_scm-springboot基于spring boot的统一注解缓存

    scm-springboot 基于spring boot的统一注解缓存,支持mencached.redis.ehcache的缓存无缝切换.支持单个缓存设置过期时间,灵活的key设置规则,采用fastj ...

  6. 在Spring Boot中使用数据缓存

    关注公众号[江南一点雨],专注于 Spring Boot+微服务以及前后端分离等全栈技术,定期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货! 春节就要到了,在回家之前要 ...

  7. spring boot使用自带缓存

    项目地址:https://gitee.com/indexman/spring_boot_in_action 下面就介绍一下如何使用spring boot自带的缓存.按步骤来操作即可,不懂的可以去看项目 ...

  8. Spring Boot 最佳实践(三)模板引擎FreeMarker集成

    一.FreeMaker介绍 FreeMarker是一款免费的Java模板引擎,是一种基于模板和数据生成文本(HMLT.电子邮件.配置文件.源代码等)的工具,它不是面向最终用户的,而是一款程序员使用的组 ...

  9. Spring Boot 最佳实践(四)模板引擎Thymeleaf集成

    ## 一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供X ...

最新文章

  1. 内联式css样式,直接写在现有的HTML标签中
  2. Linux内核探讨-- 第三章
  3. 一个批量停止和启动服务的工具
  4. PPT模板中的”书签”
  5. .Net Core 认证组件源码解析
  6. 对小程序框架WePY的精简总结
  7. table表格高级用法(一)
  8. 青岛计算机学校分数线,青岛计算机应用与维修专业职业学校收费标准,物联网应用技术中专学校分数线...
  9. linux根目录数量限制,windows,linux文件夹下文件上限最大个数
  10. 人工智能与机器学习大牛们的blog
  11. windows 自定义安装包界面
  12. 基于机器学习方法对销售预测的研究
  13. 中国食品甘油行业市场供需与战略研究报告
  14. mysql 参照完整性规则_mysql数据的完整性约束(完整)
  15. Android App赞赏功能,微信公众号赞赏功能升级:作者可以直接收到赞赏
  16. 计算机网络定义记不住,记不住是怎么回事?——记忆是个神奇的小东西
  17. 分类变量要编码成哑变量_停止对分类变量进行热编码
  18. PlantUML integration
  19. LifeKeeper软件介绍
  20. 运营商抗击微信 神州泰岳股价经历过山车

热门文章

  1. ssl1197-质数和分解【dp练习】
  2. C++ __gnu_pbds(hash,可并堆,平衡树)
  3. codeforces1472 G. Moving to the Capital
  4. 【二分】防具布置/秦腾与教学评估(ybtoj 二分-1-2/jzoj 1253/luogu 4403)
  5. HDU5765 Bonds (高维前缀和)
  6. SpringCloud Zuul(七)之POST Filter
  7. 一个非常好的依存句法可视化工具
  8. 有一种陪伴不在身边,却在心间
  9. set注意点map遍历
  10. MyBatis_1 简介