因为SpringBoot默认静态资源访问路径在资源目录下的static文件夹下,所以如果直接在页面中如下方式引入js文件将会报错:
<script type="text/javascript" src="/static/layui.js"></script>
应改为:
<script type="text/javascript" src="/layui.js"></script>
若使用的是thymeleaf模板还可以使用如下方式:
<script type="text/javascript" th:src="@{/layui.js}"></script>
ps:这里有个非常坑的就是错误写法idea不报错:
正确写法会给警告:
当然也可以自己在配置文件中修改默认访问静态资源的目录:
#SpringBoot中默认配置
spring:
mvc:
static-path-pattern: /**
resources:
static-locations: classpath:/META-INF/resources, classpath:/resources, classpath:/static, classpath:/public
#可以自定义更改为
spring:
mvc:
static-path-pattern: /**
resources:
static-locations: classpath:/