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>

 

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