首先在配置完成thymeleaf之后,要在主配置文件中,配置好静态资源的文件配置:
- spring:
- mvc:
- static-path-pattern: /static/** #静态资源的请求方式
- resources:
- static-locations: classpath:/static/ # 静态资源的配置位置,可以写成一个数组配置多个目录
这时,你所有的直接指向static目录的请求都会指向到这个目录里。
thymeleaf中,对资源路径进行配置:
- <link rel="stylesheet" type="text/css" th:href="@{/static/css/button.css}" />
- <script type="text/javascript" th:src="@{/static/js/button.js}"></script>
我踩的一个坑,没有写成 “ /static/**” 而是写成了 “ static/**”
这样的话,就会把你的上下文加到你的路径上,导致访问路径变成你的请求上下文+资源路径,以至于404.
No Leanote account? Sign up now.