今天在引入bootstrap的时候死活调用不出样式 记录一下踩的坑:
在head中代码如下
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 --> <script th:src="@{/static/jquery/jquery-3.2.1.min.js}"></script> <script th:src="@{/static/js/bootstrap.min.js}"></script>明明文件都引入了,却还是出问题。
最终排查结果如下,css样式中少了一句
<link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}"> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 --> <script th:src="@{/static/jquery/jquery-3.2.1.min.js}"></script> <script th:src="@{/static/js/bootstrap.min.js}"></script>同时,在thymeleaf中,引入url时写法格式应为: @{…} 这样可以帮助我们 自动加上那个 contextpath
如:我们在applications.properties中加上访问路径
server.servlet.context-path=/root访问路径变了会导致访问不到 css 和 js 文件,使用如上写法就不会。
