使用 Spring Boot 和 Thymeleaf 加载静态资源 [英] Loading static resources with Spring Boot and Thymeleaf
问题描述
我想加载我的静态资源.首先我认为它已经可以工作了,但这只是浏览器缓存的一个技巧.我只按预期加载了 html 文件,但我没有得到 js、css、图像等.
I want to get my static resources loaded. First I thought it already works, but that was just a trick of browser cache. I only get the html-files loaded as expected, but I dont get js, css, images and so on.
======
我的入门课程:
@Configuration
@Import({ ServiceConfig.class, WebMvcConfig.class })
@EnableHypermediaSupport(type = HAL)
@EnableAutoConfiguration
public class ApplicationClientMvc {
public static void main(final String[] args) {
SpringApplication.run(ApplicationClientMvc.class, args);
}
}
======
WebMvcConfig
@Configuration
@ComponentScan
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Autowired public SpringTemplateEngine templateEngine;
@Bean
public ThymeleafTilesConfigurer tilesConfigurer() {
final ThymeleafTilesConfigurer configurer = new ThymeleafTilesConfigurer();
configurer.setDefinitions("classpath*:/templates/**/views.xml");
return configurer;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
final ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setViewClass(ThymeleafTilesView.class);
resolver.setTemplateEngine(templateEngine);
resolver.setCharacterEncoding(UTF_8);
return resolver;
}
@Bean
public TilesDialect tilesDialect() {
return new TilesDialect();
}
//
@Value("${server.session-timeout}") private Long sessionTimeOut;
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(sessionTimeOut * 1000L);
configurer.registerCallableInterceptors(timeoutInterceptor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
}
======
我的项目资源
======
获取资源
尝试获取资源的方式不同,没有一个有效!!!
Different styles of trying to get resource, no of them works!!!
推荐答案
好的,这对我有帮助:
在 WebMvcConfig 中,我将 WebMvcConfigurationSupport
更改为 WebMvcAutoConfigurationAdapter
In WebMvcConfig I changed WebMvcConfigurationSupport
to WebMvcAutoConfigurationAdapter
如果您想了解有关该模块的更多信息,可以找到更好的概述 Stackoverflow-Link
If you want to know about more about that module, better overview you find Stackoverflow-Link
这篇关于使用 Spring Boot 和 Thymeleaf 加载静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!