javascript
Spring Boot –如何跳过缓存thyemeleaf模板,js,css等以每次绕过重启服务器
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(不會覆蓋任何內容) 
希望這可以幫助!
翻譯自: https://www.javacodegeeks.com/2019/04/skip-cache-thyemeleaf-bypass-restarting-server.html
總結
以上是生活随笔為你收集整理的Spring Boot –如何跳过缓存thyemeleaf模板,js,css等以每次绕过重启服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 己烷的同分异构体知识点 己烷的同分异构体
- 下一篇: 预期成果是什么意思 预期成果的意思
