1. 程式人生 > >Spring boot(17) thymeleaf 使用類似於include的方式

Spring boot(17) thymeleaf 使用類似於include的方式

1.建立模板

secure_template.html

<!DOCTYPE html>
<!-- 僅作為模板使用,服務端不要直接返回 -->
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>許可權center</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet"
type="text/css" th:href="@{/mg/css/table.css}" />
<link rel="stylesheet" type="text/css" th:href="@{/mg/css/public.css}" /> <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> <link href="//cdn.bootcss.com/bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"
/>
<script src="//cdn.bootcss.com/bootstrap/3.0.1/js/bootstrap.js"></script> </head> <body> <div id="wrapper"> <nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation"> <div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <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="#"><span class="glyphicon glyphicon-home">Home</span></a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="/mg/sercure_roles">角色列表</a></li> <li><a href="/mg/sercure_accounts">使用者列表</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">監控<strong class="caret"></strong></a> <ul class="dropdown-menu"> <li><a href="#">使用者</a></li> <li><a href="#">角色</a></li> <li class="divider"></li> <li><a href="#">日誌監控</a></li> </ul> </li> </ul> </div> </nav> <div style="margin-top: 5%" th:include="this :: content"></div> <div id="footer"> @ gaotianyue </div> </div> </body> </html>

其中最重要的是th:include=”this :: content”

2使用方法

<!DOCTYPE html>
<html lang="zh-CN"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="secure_template">
<head>
<title>角色管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<link rel="stylesheet" type="text/css" th:href="@{/mg/css/table.css}" />
<link rel="stylesheet" type="text/css" th:href="@{/mg/css/public.css}" />
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<link href="//cdn.bootcss.com/bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//cdn.bootcss.com/bootstrap/3.0.1/js/bootstrap.js"></script>
    <body>
        <div th:fragment="content" >
            <!-- 自己的內容 -->
            <div class="container"> 
            </div>
        </div>
    </body>
</html>

其中最重要的是 layout:decorator=”secure_template”
div th:fragment=”content”