一.包括模板片斷:html
1:定義和引用片斷,咱們常常會想要包含在模板片斷來自其餘模板。常見的用途是頁腳、標題、菜單…;dom
爲了作到這一點,Thymeleaf須要咱們定義包含可用的片斷,咱們能夠經過使用th:fragment屬性。spa
定義一個頁面底部footer頁面,在每個須要的頁面均可以用的模板,能夠經過使用th:fragment屬性code
<div th:fragment="copy"> © 2014 The Good Thymes Virtual Grocery </div>
上面的代碼定義了一個叫作副本的片斷,咱們能夠很容易地包含在咱們的主頁上經過使用th:include
or th:replace屬性
之一:orm
<body> ... <div th:include="footer :: copy"></div> </body>
引用片斷沒有th:fragment:htm
... <div id="copy-section"> © 2011 The Good Thymes Virtual Grocery </div> ...
頁面引用:th:include="templatename::domselector"blog
templatename是要引入頁面的路勁加上去掉後綴的名稱,例如footer.html頁面創建在/WEB-INF/templates/footer.html,因此templatename爲footer;domselector就是dom選擇器,即爲th:fragment中的值,或是選擇id
<body> ... <div th:include="footer :: #copy-section"></div> </body
注意:it
帶有公共的頁面,不要帶有 <html> <head></head> <body></body> </html> 直接寫內容: <div th:fragment="copy"> © 2011 The Good Thymes Virtual Grocery </div>
擴展寫法,但願能靈活運用:io
<div th:include="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>
二.可參數化的片斷簽名模板
能夠像參數同樣傳入參數:
<div th:fragment="frag (onevar,twovar)"> <p th:text="${onevar} + ' - ' + ${twovar}">...</p> </div>
兩種調用方式引入頁面:
<div th:include="::frag (${value1},${value2})">...</div> <div th:include="::frag (onevar=${value1},twovar=${value2})">...</div>
若是沒有帶參數,以下形式:
<div th:fragment="frag"> ... </div>
依然可使用帶參數的引入,可是必須使用第二種引入方式,另外一種不行:以下是正確的引入方式
<div th:include="::frag (onevar=${value1},twovar=${value2})">
這樣事實上,這將是至關於一個th:include
和th:with的
組合
<div th:include="::frag" th:with="onevar=${value1},twovar=${value2}">