我尝试了在这里和文档中找到的所有提示和技巧,但仍然没有成功。我有带有 Thymeleaf 的 Spring webapp。当我在 IDEA 中调用更新时,资源和模板不会重新加载(它没有说明要重新加载)。然后,我可以疯狂地在浏览器中按 ctrl+f5,但没有任何更改。
一切都在一个 Java 类中配置,如下所示:
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
我的文件夹结构现在看起来像 this ,但我也尝试将没有“static”文件夹的资源或放到 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 Boot hot swap
- SO - Spring Boot + Jetty & hot deployment
- SO - Netbeans 8 won't reload static Thymeleaf files
我最初的方法是禁用缓存并添加 Spring 开发工具:
Spring Boot 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;
}
}
我发现这个解决方案是最佳的,因为它允许您外部化配置并使用不同的配置文件(dev、prod 等),同时通过只需按 F5 :) 即可重新加载更改
关于java - Spring boot 和 Thymeleaf - 再次热交换模板和资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40057057/