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

时间:2015-01-09 21:10:07

标签: configuration spring-boot thymeleaf static-resource

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

==

我的项目资源

enter image description here

=====

访问资源

尝试获取资源的不同风格,其中没有一种工作!!!

enter image description here

2 个答案:

答案 0 :(得分:6)

好的,这帮助了我:

在WebMvcConfig中,我将WebMvcConfigurationSupport更改为WebMvcAutoConfigurationAdapter

enter image description here

如果您想了解有关该模块的更多信息,请查看更好的概述Stackoverflow-Link

答案 1 :(得分:5)

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