简体   繁体   English

Spring boot 和 Thymeleaf - 再次热插拔模板和资源

[英]Spring boot and Thymeleaf - Hot swap templates and resources once again

I tried all tips and tricks that I found here and in docs, but still no luck.我尝试了在这里和文档中找到的所有提示和技巧,但仍然没有运气。 I have Spring webapp with Thymeleaf.我有带有 Thymeleaf 的 Spring webapp。 Resources and templates are not reloaded when I call update in IDEA (it says nothing to reload).当我在 IDEA 中调用 update 时,不会重新加载资源和模板(它没有说明要重新加载)。 I can then press ctrl+f5 in a browser like crazy, changes are just not there.然后我可以在浏览器中疯狂地按 ctrl+f5,只是不存在更改。

Everything is configured in one Java class like this:一切都在一个 Java 类中配置,如下所示:

@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

My folder structure now looks like this , but I also tried to put the resources without "static" folder or to webapp/resources.我的文件夹结构现在看起来像这样,但我也尝试将没有“静态”文件夹的资源或 webapp/resources.

ResourceHandlerRegistry:资源处理程序注册表:

@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/");
}

I specified cache=false in both application.properties:我在两个 application.properties 中都指定了 cache=false:

spring.thymeleaf.cache=false

and in mentioned MvcConfig class:并在提到的 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;
}

According to some answers on SO i added dependency for devtools:根据一些关于SO的答案,我添加了对devtools的依赖:

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

Still not working.还是行不通。 Some said to add maven boot plugin with addResources=true, so I did:有人说用 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>

My Idea is set properly I guess, because when I call update, my Java classes are reloaded immediately.我猜我的想法设置正确,因为当我调用更新时,我的 Java 类会立即重新加载。 Only resources and html files are not, I must restart server for it.只有资源和html文件不是,我必须为它重新启动服务器。 Actualy *.html files are not so big a deal, but to restart server after every small css and js change is slowing me down a lot, and as I lost almost 15 hours figuring out what is wrong, it started to be really frustrating.实际上 *.html 文件并不是什么大不了的事,但是在每次小的 css 和 js 更改后重新启动服务器让我的速度变慢了很多,而且我花了将近 15 个小时来找出问题所在,这开始非常令人沮丧。

Any help will be greatly appreciated.任何帮助将不胜感激。

I have spent some time on it and finally here I'll explain how I got it working.我已经花了一些时间,最后在这里我将解释我是如何让它工作的。 Googling around you may find several info:谷歌搜索你可能会发现几个信息:

My inital approach was to disable caching and add Spring dev tools:我最初的方法是禁用缓存并添加 Spring 开发工具:

Spring boot application.properties春季启动application.properties

spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=/templates/

pom.xml pom.xml

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

Using the snippet above however is not enough since the hot swap is done only when making the project (CTRL + F9 in Intellij Idea).然而,使用上面的代码片段是不够的,因为热交换仅在制作项目时完成(Intellij Idea 中的 CTRL + F9)。 This is due to the fact that the default template resolver is classpath based and that's the reason a recompilation is necessary.这是因为默认模板解析器是基于类路径的,因此需要重新编译。


A working solution is to override the defaultTemplateResolver by using a file system based resolver:一个可行的解决方案是使用基于文件系统的解析器覆盖defaultTemplateResolver

application.properties应用程序属性

spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.templates_root=src/main/resources/templates/

Application class应用类

@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;
    }
}

I find this solution optimal since it allow you to externalize the configuration and use different profiles (dev, prod, etc..) while having the benefit of reloading the changes by just pressing F5 :)我发现此解决方案是最佳的,因为它允许您将配置外部化并使用不同的配置文件(开发、产品等),同时只需按 F5 即可重新加载更改:)

Here are my settings with IntelliJ IDEA (2018.3), it's reload HTML after the changes are saved:这是我对 IntelliJ IDEA (2018.3) 的设置,它会在保存更改后重新加载 HTML:

  1. In application.properties:在 application.properties 中:

     spring.resources.static-locations = classpath:/resources/static spring.resources.cache.period = 0
  2. In pom.xml, set <addResources>true</addResources>在 pom.xml 中,设置<addResources>true</addResources>

     <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <addResources>true</addResources> </configuration> </plugin>
  3. Menu Run => Edit Configurations (IntelliJ IDEA)菜单Run => Edit Configurations (IntelliJ IDEA)

On frame deactivation: Update resources帧停用: Update resources

Okay, so I found answer to my specific case.好的,所以我找到了针对我的具体案例的答案。 Problem was not at all in my app or it's config (well.. probably).问题根本不在我的应用程序或其配置中(嗯..可能)。 Instead of using Tomcat 8.5.5 I switched back to Tomcat 7 .我没有使用Tomcat 8.5.5 ,而是切换回Tomcat 7 Everything now works properly.现在一切正常。 Does somebody know why?有人知道为什么吗?

@Luke my solution was quite simple: @Luke 我的解决方案非常简单:

To have HTML / CSS / JS reloading automatically in Spring Thymeleaf can be simple and bug free, have only tested in IntelliJ.在 Spring Thymeleaf 中自动重新加载 HTML / CSS / JS 可以简单且无错误,仅在 IntelliJ 中进行了测试。

Add this to maven, use ${spring.version} var or replace by your version:将此添加到 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>

Add to the header of the html:添加到 html 的标题中:

<script src="http://localhost:35729/livereload.js"></script>

When using IntelliJ:使用 IntelliJ 时:

使用 Ctrl+Shift+A 写入注册表

在 IntelliJ 设置中

The simplest solution is to configure Spring's Thymeleaf template source to load from the filesystem, rather than the classpath.最简单的解决方案是将 Spring 的 Thymeleaf 模板源配置为从文件系统加载,而不是从类路径加载。

This is a super-simple -- esentially a one-liner in config -- but seems little-known on the Internet.这是一个超级简单的——本质上是一个单行配置——但在互联网上似乎鲜为人知。 Credit to @JianrongChen's for posting it in his comment.感谢@JianrongChen 在他的评论中发布它。

application.properties应用程序属性

# 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

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM