SpringBoot学习(六)使用Thymeleaf布局

SpringBoot使用Thymeleaf布局

一、在项目中引入bootstrap

在resources、static下创建bootstrap目录,static目录下存放静态内容。将从Bootstrap官网下载的文件解压后,将其CSS、js、fonts三个文件夹复制到bootstrap目录下。

将从jQuery官网下载的jquery.min.js文件复制到bootstrap的js文件夹中。

项目结构如下:

二、创建布局页

1。在templates下创建common文件夹

2。在该文件夹下创建layout.html文件,内容如下

<!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>

重点是该部分页面主体部分

<!-- 页面主体部分-->
    <div id="main_content" th:include="::main_content">

    </div>

该部分的具体内容,将来是由内容页呈现的,其余的内容是布局页中的内容。

三、在布局页中引入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>

四、创建内容页,引入布局页

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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章