【问题标题】:Loading static resources with Spring Boot and Thymeleaf使用 Spring Boot 和 Thymeleaf 加载静态资源
【发布时间】:2015-03-08 06:54:30
【问题描述】:

我想加载我的静态资源。首先我认为它已经可以工作了,但这只是浏览器缓存的一个技巧。我只按预期加载了 html 文件,但没有加载 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();
    }
}

======

我的项目资源

=====

访问资源

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

【问题讨论】:

    标签: configuration spring-boot thymeleaf static-resource


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,它帮助将 WebMvcConfigurationSupport 更改为 WebMvcAutoConfigurationAdapter,然后找到了资源。但我不知道为什么......
    【解决方案2】:

    好的,这对我有帮助:

    在 WebMvcConfig 中,我将 WebMvcConfigurationSupport 更改为 WebMvcAutoConfigurationAdapter

    如果您想了解有关该模块的更多信息,请参阅*-Link 更好的概述

    【讨论】:

      最近更新 更多