Spring Boot 的 jsp文件中无法引入静态资源文件(js,css等文件),错误(net::ERR_ABORTED 404)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wyx_wx/article/details/83515482

报错信息:

报错时我的jsp文件中引入路径:

<script type="text/javascript" src="../../sources/static/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../sources/static/websocket.js"></script>

正确路径:删除掉static及其之前的全部路径

<script type="text/javascript" src="/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/websocket.js"></script>

原因:

在未对 application.properties 文件进行配置时,Spring Boot 默认从resources目录的static目录查找资源文件,所以 src 目录中无需加入 static 及其之前的路径

若想对资源路径进行配置,可以在 application.properties 配置文件中加入下面的配置

spring.mvc.static-path-pattern=/**

猜你喜欢

转载自blog.csdn.net/Wyx_wx/article/details/83515482