[英]Hot Reloading Thymeleaf Templates and Resource Bundles in Spring Boot 2.1
[英]Spring boot and Thymeleaf - Hot swap templates and resources once again
我嘗試了在這里和文檔中找到的所有提示和技巧,但仍然沒有運氣。 我有帶有 Thymeleaf 的 Spring webapp。 當我在 IDEA 中調用 update 時,不會重新加載資源和模板(它沒有說明要重新加載)。 然后我可以在瀏覽器中瘋狂地按 ctrl+f5,只是不存在更改。
一切都在一個 Java 類中配置,如下所示:
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
我的文件夾結構現在看起來像這樣,但我也嘗試將沒有“靜態”文件夾的資源或 webapp/resources.
資源處理程序注冊表:
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/img/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
}
我在兩個 application.properties 中都指定了 cache=false:
spring.thymeleaf.cache=false
並在提到的 MvcConfig 類中:
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
return templateResolver;
}
根據一些關於SO的答案,我添加了對devtools的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.4.1.RELEASE</version>
<optional>true</optional>
</dependency>
還是行不通。 有人說用 addResources=true 添加 maven 啟動插件,所以我做了:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
我猜我的想法設置正確,因為當我調用更新時,我的 Java 類會立即重新加載。 只有資源和html文件不是,我必須為它重新啟動服務器。 實際上 *.html 文件並不是什么大不了的事,但是在每次小的 css 和 js 更改后重新啟動服務器讓我的速度變慢了很多,而且我花了將近 15 個小時來找出問題所在,這開始非常令人沮喪。
任何幫助將不勝感激。
我已經花了一些時間,最后在這里我將解釋我是如何讓它工作的。 谷歌搜索你可能會發現幾個信息:
我最初的方法是禁用緩存並添加 Spring 開發工具:
春季啟動application.properties
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=/templates/
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
然而,使用上面的代碼片段是不夠的,因為熱交換僅在制作項目時完成(Intellij Idea 中的 CTRL + F9)。 這是因為默認模板解析器是基於類路徑的,因此需要重新編譯。
一個可行的解決方案是使用基於文件系統的解析器覆蓋defaultTemplateResolver
:
應用程序屬性
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.templates_root=src/main/resources/templates/
應用類
@SpringBootApplication
public class MyApplication {
@Autowired
private ThymeleafProperties properties;
@Value("${spring.thymeleaf.templates_root:}")
private String templatesRoot;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public ITemplateResolver defaultTemplateResolver() {
FileTemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(properties.getSuffix());
resolver.setPrefix(templatesRoot);
resolver.setTemplateMode(properties.getMode());
resolver.setCacheable(properties.isCache());
return resolver;
}
}
我發現此解決方案是最佳的,因為它允許您將配置外部化並使用不同的配置文件(開發、產品等),同時只需按 F5 即可重新加載更改:)
這是我對 IntelliJ IDEA (2018.3) 的設置,它會在保存更改后重新加載 HTML:
在 application.properties 中:
spring.resources.static-locations = classpath:/resources/static spring.resources.cache.period = 0
在 pom.xml 中,設置<addResources>true</addResources>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <addResources>true</addResources> </configuration> </plugin>
菜單Run
=> Edit Configurations
(IntelliJ IDEA)
幀停用: Update resources
好的,所以我找到了針對我的具體案例的答案。 問題根本不在我的應用程序或其配置中(嗯..可能)。 我沒有使用Tomcat 8.5.5 ,而是切換回Tomcat 7 。 現在一切正常。 有人知道為什么嗎?
@Luke 我的解決方案非常簡單:
在 Spring Thymeleaf 中自動重新加載 HTML / CSS / JS 可以簡單且無錯誤,僅在 IntelliJ 中進行了測試。
將此添加到 maven,使用 ${spring.version} var 或替換為您的版本:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring.version}</version>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
添加到 html 的標題中:
<script src="http://localhost:35729/livereload.js"></script>
使用 IntelliJ 時:
最簡單的解決方案是將 Spring 的 Thymeleaf 模板源配置為從文件系統加載,而不是從類路徑加載。
這是一個超級簡單的——本質上是一個單行配置——但在互聯網上似乎鮮為人知。 感謝@JianrongChen 在他的評論中發布它。
應用程序屬性
# Thymeleaf templates from filesystem, not cached
#
spring.thymeleaf.cache=false
spring.thymeleaf.templates_root=file:///${user.dir}/src/main/resources/templates/
# static content from filesystem first, too
#
spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.