根据文档Spring Boot为静态资源位置注册src/main/resources/static
,src/main/resources/public
,src/main/resources/resources
等。
我得到了
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");
}
}
我得到了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
POM中的。
答案 0 :(得分:6)
@EnableWebMvc
的显式声明禁用Spring Boot的Web MVC自动配置。
如果您不想启用自动配置,请从WebMvcConfig
中删除它以启用自动配置,或在addResourceHandlers()
中手动添加静态内容的资源处理程序。