2015-01-09 191 views
3

我想加載靜態資源。首先,我認爲它已經有效,但這只是瀏覽器緩存的一個技巧。我只收到按預期方式加載的html文件,但我沒有獲取js,css,圖像等。使用Spring Boot和Thymeleaf加載靜態資源

======

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

==== ==

我的項目Resoureces

enter image description here

=====

訪問resoureces

不同風格的嘗試獲取資源,沒有他們的工作!

enter image description here

回答

5

好了,幫我:

在WebMvcConfig我改變WebMvcConfigurationSupportWebMvcAutoConfigurationAdapter

enter image description here

如果您想了解更多關於該模塊,更好地瞭解您發現Stackoverflow-Link

5

你並不需要在路徑static。嘗試/css/bbs-login.css

+0

謝謝爲了您的答案,它幫助將WebMvcConfigurationSupport更改爲WebMvcAutoConfigurationAdapter,然後找到資源。但我不知道爲什麼... – 2015-01-09 22:21:52

+0

你的建議節省了我的時間。謝謝! – 2017-07-27 15:02:28