首先看一下资源目录:
我想把dashboard.html里面的资源作为公共资源让emp下的list.html引用。怎么做?
比如我想把dashboard.html里面的头部拿来共用:
在dashboard.html里找到头部资源,加上 th:fragment="topbar" 作为公共头部
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">[[${session.loginUser}]]</a>
<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
<ul class="navbar-nav px-3">
<li class="nav-item text-nowrap">
<a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Sign out</a>
</li>
</ul>
</nav>
之后来到list.html页面,把相同代码删掉
引入公共资源:
<!--引入公共的代碼-->
<div th:replace="dashboard::topbar"></div>
Ctrl+F9 重新编译页面(不用重启)
查看list.html页面效果
为了以后更好的拓展,可以吧公共代码放在一个文件夹下:
之后把页面直接引入就行了:
commons/bar:模板名,与页面名一致 topbar:被引入部分名
<div th:replace="commons/bar::topbar"></div>