繁体   English   中英

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

[英]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:

  1. 在 application.properties 中:

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

     <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <addResources>true</addResources> </configuration> </plugin>
  3. 菜单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 时:

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

在 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.

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