日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot –如何跳过缓存thyemeleaf模板,js,css等以每次绕过重启服务器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring Boot自動配置為ThyemeLeaf注冊的默認(rèn)模板解析器是基于類路徑的,這意味著它從編譯的資源/ target / classes / **加載模板和其他靜態(tài)資源。

要加載對資源(HTML,js,CSS等)的更改,我們可以

  • 每次都重新啟動應(yīng)用程序-這當(dāng)然不是一個好主意!
  • 使用IntelliJ上的CTRL + F9或(如果您使用的是eclipse鍵映射,則使用CTRL + SHIFT + F9)或只是右鍵單擊并單擊編譯來重新編譯資源。
  • 或如下所述的更好的解決方案!

Thymeleaf包括一個基于文件系統(tǒng)的解析器,它直接從文件系統(tǒng)中加載模板,而無需通過類路徑(已編譯資源)。

請參見DefaultTemplateResolverConfiguration#defaultTemplateResolver中的代碼段

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

屬性前綴默認(rèn)為“ classpath:/ template /”。 參見摘要ThymeleafProperties#DEFAULT_PREFIX

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

解決方案:

Spring Boot允許我們覆蓋屬性“ spring.thymeleaf.prefix”以指向源文件夾“ src / main / resources / templates /”,而不是默認(rèn)的“ classpath:/ templates /”。

在application.yml | properties文件中:

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

這將告訴運行時不要查看目標(biāo)/文件夾。 而且您不需要每次在我們的src / main / resources / template上更新html模板時都重新啟動服務(wù)器

那么JavaScript / CSS文件呢?

您可以進(jìn)一步繼續(xù)更新“ spring.resources.static-locations”以指向您的靜態(tài)資源文件夾(保存js / css,圖像等)

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

完整代碼:

好的做法是僅在開發(fā)過程中具有上述配置。 要具有生產(chǎn)系統(tǒng)的默認(rèn)配置,可以使用“個人檔案”并為每個環(huán)境定義單獨的行為。

這是基于我們剛剛描述的完整代碼段!

項目結(jié)構(gòu):

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(不會覆蓋任何內(nèi)容)

spring: profiles: active: prod

希望這可以幫助!

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

總結(jié)

以上是生活随笔為你收集整理的Spring Boot –如何跳过缓存thyemeleaf模板,js,css等以每次绕过重启服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。