使用 Spring Boot 和 Thymeleaf 加载静态资源

Loading static resources with Spring Boot and Thymeleaf

我想加载我的静态资源。一开始我认为它已经可以工作了,但这只是浏览器缓存的一个技巧。我只按预期加载了 html-files,但没有获取 js、css、图像等。

======

我的StartClass:

@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();
    }
}

======

我的项目资源

=====

访问资源

尝试获取资源的不同方式,没有一种有效!!!

路径中不需要 static。试试 /css/bbs-login.css.

好的,这对我有帮助:

在 WebMvcConfig 中,我将 WebMvcConfigurationSupport 更改为 WebMvcAutoConfigurationAdapter

如果您想了解有关该模块的更多信息,可以找到更好的概述 Whosebug-Link