SpringBoot使用Thymeleaf布局css
1、在项目中引入bootstraphtml
在resources、static下建立bootstrap目录,static目录下存放静态内容。将从Bootstrap官网下载的文件解压后,将其CSS、js、fonts三个文件夹复制到bootstrap目录下。jquery
将从jQuery官网下载的jquery.min.js文件复制到bootstrap的js文件夹中。bootstrap
项目结构以下:布局
2、建立布局页测试
1。在templates下建立common文件夹ui
2。在该文件夹下建立layout.html文件,内容以下spa
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title th:text="${title}">Title</title> <!--引入BootsStrap样式--> <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.css}"> <!--引入自定义样式--> <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/main.css}"> <!--引入js文件--> <script th:src="@{/bootstrap/js/jquery.min.js}"></script> <script th:src="@{/bootstrap/js/bootstrap.js}"></script> </head> <body> <!--最外层容器--> <div id="wrap" class="container"> <!--页面头部--> <header> <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Brand</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"> <a href="#" th:href="@{/}">首页</a> </li> <li><a href="#" th:href="@{/test}">测试</a></li> <li><a href="#" th:href="@{/article}">技术文章</a></li> </ul> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text" class="form-control" placeholder="请输入文章名"> </div> <button type="submit" class="btn btn-default">Search</button> </form> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> </header> <!-- 页面主体部分--> <div id="main_content" th:include="::main_content"> </div> <!--页面底部--> <footer class="row"> <div class="col-md-12"> <ul> <li><a href="#">首页</a></li> <li><a href="#">首页</a></li> <li><a href="#">首页</a></li> <li><a href="#">首页</a></li> </ul> </div> <div class="col-md-12"> <p> CopyRight:BlueMonkey 地址:中国河南 </p> </div> </footer> </div> </body> </html>
重点是该部分页面主体部分code
<!-- 页面主体部分--> <div id="main_content" th:include="::main_content"> </div>
该部分的具体内容,未来是由内容页呈现的,其他的内容是布局页中的内容。orm
3、在布局页中引入CSS和js文件
在布局页中引入CSS和js文件,主要注意其路径便可。
<!--引入BootsStrap样式--> <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.css}"> <!--引入自定义样式--> <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/main.css}"> <!--引入js文件--> <script th:src="@{/bootstrap/js/jquery.min.js}"></script> <script th:src="@{/bootstrap/js/bootstrap.js}"></script>
4、建立内容页,引入布局页
1。建立内容页,项目结构以下
2。内容页内容以下
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="common/layout(title='测试布局')"> <div th:fragment="main_content"> <table class="table table-hover"> <tr> <td>编号</td> <td>标题</td> <td>发表日期</td> </tr> <tr th:each="article:${articles}"> <td th:text="${article.getId()}"></td> <td th:text="${article.getTitle()}"></td> <td th:text="${article.getCreatedate()}"></td> </tr> </table> </div> </html>