2014-04-21 242 views
1

根据文档Spring Boot寄存器src/main/resources/staticsrc/main/resources/public,src/main/resources/resources等查找静态资源位置。Spring Boot Web静态资源

src/main/resources 
    -static 
     -js 
      -stomp.js 

,并在HTML(放置在模板文件夹):

<script type="text/javascript" th:src="@{/js/stomp.js}"></script> 

但我得到了404的脚本(GET http://localhost:8080/js/stomp.js 404 (Not Found))。我做错了什么?

我也试过

<script type="text/javascript" src="../static/js/stomp.js"></script> 

<script type="text/javascript" src="/js/stomp.js"></script> 

具有相同的结果。

我的MVC配置(仅一个视图 - 控制器):

@EnableWebMvc 
@ComponentScan(basePackages = {"controller"}) 
@Configuration 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/sock").setViewName("/index"); 
    } 
} 

和i。在POM得到

 <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

+0

http://stackoverflow.com/questions/17312034/cant-find-css-images-and-js-static-files-in-spring – Thunda

回答

6

明确声明@EnableWebMvc会禁用Spring Boot的Web MVC自动配置。

如果您不想启用自动配置,请将其从WebMvcConfig中删除以启用自动配置,或者在addResourceHandlers()中手动为静态内容添加资源处理程序。

+0

谢谢...这真的很难找到。 – user152468