【发布时间】:2017-02-24 16:56:39
【问题描述】:
我尝试了在此处和文档中找到的所有提示和技巧,但仍然没有运气。我有带有 Thymeleaf 的 Spring webapp。当我在 IDEA 中调用 update 时,不会重新加载资源和模板(它没有说明要重新加载)。然后我可以在浏览器中疯狂地按 ctrl+f5,只是没有更改。
一切都在一个 Java 类中配置,如下所示:
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
我的文件夹结构现在看起来像 this,但我也尝试将没有“静态”文件夹的资源放入 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/");
}
我在两个 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 个小时来找出问题所在,这开始非常令人沮丧。
任何帮助将不胜感激。
【问题讨论】:
-
我刚刚发现更改实际上反映在“目标”文件夹中,而不是正在运行的应用程序本身中。
标签: java spring-mvc spring-boot thymeleaf hotswap