Luk*_*uke 6 java spring-mvc hotswap thymeleaf spring-boot
我尝试了我在这里和文档中找到的所有提示和技巧,但仍然没有运气.我有Thymeleaf的Spring webapp.当我在IDEA中调用update时,不会重新加载资源和模板(它没有重新加载).然后,我可以在浏览器中按ctrl + f5疯狂,更改不存在.
一切都在一个Java类中配置,如下所示:
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
Run Code Online (Sandbox Code Playgroud)
我的文件夹结构现在看起来像 这样,但我也尝试将资源放在没有"静态"文件夹或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/");
}
Run Code Online (Sandbox Code Playgroud)
我在application.properties中指定了cache = false:
spring.thymeleaf.cache=false
Run Code Online (Sandbox Code Playgroud)
并在提到的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;
}
Run Code Online (Sandbox Code Playgroud)
根据SO的一些答案我添加了对devtools的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.4.1.RELEASE</version>
<optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)
还是行不通.有人说用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>
Run Code Online (Sandbox Code Playgroud)
我的想法设置正确,因为当我调用update时,我的Java类会立即重新加载.只有资源和html文件不是,我必须为它重新启动服务器.Actualy*.html文件并不是什么大不了的事,但是在每次小css和js更改之后重新启动服务器会让我失望很多,而且当我丢失了将近15个小时弄清楚什么是错误时,它开始真的很令人沮丧.
任何帮助将不胜感激.
Pai*_*izo 15
我花了一些时间在这里,最后在这里我将解释我是如何工作的.谷歌搜索你可能会找到几个信息:
我的初衷是禁用缓存并添加Spring开发工具:
春天的靴子 application.properties
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=/templates/
Run Code Online (Sandbox Code Playgroud)
的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是,使用上面的代码段是不够的,因为只有在制作项目时才进行热交换(Intellij Idea中的CTRL + F9).这是因为默认模板解析器是基于类路径的,这就是需要重新编译的原因.
一个有效的解决方案是defaultTemplateResolver
使用基于文件系统的解析器来覆盖:
application.properties
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.templates_root=src/main/resources/templates/
Run Code Online (Sandbox Code Playgroud)
应用类
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我发现这个解决方案是最优的,因为它允许你外化配置并使用不同的配置文件(dev,prod等等),同时只需按F5即可重新加载更改:)
归档时间: |
|
查看次数: |
5989 次 |
最近记录: |