Spring Boot和Thymeleaf-再次热插拔模板和资源 [英] Spring boot and Thymeleaf - Hot swap templates and resources once again

查看:187
本文介绍了Spring Boot和Thymeleaf-再次热插拔模板和资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了所有在这里和在文档中找到的技巧和窍门,但还是没有运气.我有Thymeleaf的Spring webapp.当我在IDEA中调用update时,不会重新加载资源和模板(它说什么也没有要重新加载).然后,我可以在疯狂的浏览器中按ctrl + f5,只是不存在更改.

I tried all tips and tricks that I found here and in docs, but still no luck. I have Spring webapp with Thymeleaf. Resources and templates are not reloaded when I call update in IDEA (it says nothing to reload). I can then press ctrl+f5 in a browser like crazy, changes are just not there.

所有内容都在一个Java类中进行配置,如下所示:

Everything is configured in one Java class like this:

@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

我的文件夹结构现在看起来像 ,但我也尝试将资源放在没有静态"文件夹或webapp/的位置.资源.

My folder structure now looks like this, but I also tried to put the resources without "static" folder or to webapp/resources.

ResourceHandlerRegistry:

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

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

I specified cache=false in both application.properties:

spring.thymeleaf.cache=false

,并在提到的MvcConfig类中:

and in mentioned MvcConfig class:

@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添加了依赖项:

According to some answers on SO i added dependency for 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启动插件,所以我这样做了:

Still not working. Some said to add maven boot plugin with addResources=true, so I did:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.4.1.RELEASE</version>
    <configuration>
        <addResources>true</addResources>
    </configuration>
</plugin>

我猜我的想法设置正确,因为当我调用update时,我的Java类会立即重新加载.只有资源和HTML文件不是,我必须为此重新启动服务器.实际上* .html文件没什么大不了的,但是在每一次小的css和js更改后重新启动服务器都会使我的工作减慢很多,而且由于我花了将近15个小时来弄清楚问题出在哪里,这真让人沮丧.

My Idea is set properly I guess, because when I call update, my Java classes are reloaded immediately. Only resources and html files are not, I must restart server for it. 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.

任何帮助将不胜感激.

推荐答案

我花了一些时间,最后在这里我将解释如何使它工作. 到处搜寻您可能会发现一些信息:

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:

  • Spring Boot hot swap
  • SO - Spring Boot + Jetty & hot deployment
  • SO - Netbeans 8 won't reload static Thymeleaf files

我最初的方法是禁用缓存并添加Spring开发工具:

My inital approach was to disable caching and add Spring dev tools:

春季靴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).这是因为默认模板解析器基于类路径,这就是需要重新编译的原因.

Using the snippet above however is not enough since the hot swap is done only when making the project (CTRL + F9 in Intellij Idea). This is due to the fact that the default template resolver is classpath based and that's the reason a recompilation is necessary.

一种可行的解决方案是使用基于文件系统的解析器来覆盖defaultTemplateResolver:

A working solution is to override the defaultTemplateResolver by using a file system based resolver:

application.properties

application.properties

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

我发现此解决方案是最佳的,因为它允许您外部化配置并使用不同的配置文件(dev,prod等),同时具有通过按只需按F5来重新加载更改的好处:)

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 :)

这篇关于Spring Boot和Thymeleaf-再次热插拔模板和资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆