Thymeleaf导入其他html(th:replace & th:include)

模板模块导入

首先定义一个/WEBINF/templates/footer.html文件:


 
 
  1. <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  3. <body>
  4. <div th:fragment="copy">
  5. &copy; 2011 The Good Thymes Virtual Grocery
  6. </div>
  7. </body>
  8. </html>

上面的代码定义了一个片段称为copy,我们可以很容易地使用th:include 或者 th:replace属性包含在我们的主页上:


 
 
  1. <body>
  2. ...
  3. <div th:include= "footer :: copy" > </div>
  4. </body>

include的表达式想当简洁。这里有三种写法:

<div th:include="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>
 
 

不使用th:fragment来引用模块


 
 
  1. ...
  2. <div id= "copy-section" >
  3. &copy; 2011 The Good Thymes Virtual Grocery
  4. </div>
  5. ...

我们可以用css的选择器写法来引入


 
 
  1. <body>
  2. ...
  3. <div th:include= "footer :: #copy-section" > </div>
  4. </body>

th:include 和 th:replace的区别

th:include和th:replace都可以引入模块,两者的区别在于 
th:include:引入子模块的children,依然保留父模块的tag。 
th:replace:引入子模块的所有,不保留父模块的tag。 
举个栗子:


 
 
  1. <footer th:fragment="copy">
  2. &copy; 2011 The Good Thymes Virtual Grocery
  3. </footer>

引入界面:


 
 
  1. <body>
  2. ...
  3. <div th:include= "footer :: copy" > </div>
  4. <div th:replace= "footer :: copy" > </div>
  5. </body>

结果是:


 
 
  1. <body>
  2. ...
  3. <div>
  4. &copy; 2011 The Good Thymes Virtual Grocery
  5. </div>
  6. <footer>
  7. &copy; 2011 The Good Thymes Virtual Grocery
  8. </footer>
  9. </body>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Thymeleaf是一个流行的服务器端Java模板引擎,它提供了一套丰富的语法和标签,让开发者可以更加方便地处理HTML模板。以下是Thymeleaf的语法大全: 1. 基本语法 Thymeleaf的基本语法是使用${}表示表达式,例如: ``` <p th:text="${user.name}">John Doe</p> ``` 上面的代码会将user对象中的name属性的值显示在页面上。 2. 属性设置 Thymeleaf可以在HTML标签中添加属性,例如: ``` <img src="image.png" th:src="@{${user.avatarUrl}}" /> ``` 上面的代码中,使用@{}包裹表达式,将${user.avatarUrl}的值设置为img标签的src属性。 3. 条件判断 Thymeleaf支持if/else条件判断语句,例如: ``` <p th:if="${user.isAdmin}">Welcome Admin</p> ``` 上面的代码中,如果user.isAdmin为true,则显示“Welcome Admin”。 4. 列表循环 Thymeleaf可以通过th:each指令实现列表循环,例如: ``` <ul> <li th:each="item : ${items}" th:text="${item}"></li> </ul> ``` 上面的代码中,将items列表中的每个元素显示在li标签中。 5. 模板继承 Thymeleaf支持模板继承,可以在父模板中定义公共部分,例如: 父模板: ``` <html> <head th:fragment="head"> <title>My Website</title> <link href="style.css" rel="stylesheet" /> </head> <body> <header th:fragment="header"> <h1>My Website</h1> </header> <div th:fragment="content"></div> <footer th:fragment="footer"></footer> </body> </html> ``` 子模板: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" th:include="layout/layout :: head"> <body> <div th:include="layout/layout :: header"></div> <div th:replace="content"></div> <div th:include="layout/layout :: footer"></div> </body> </html> ``` 上面的代码中,子模板使用th:include指令引入父模板的公共部分,使用th:replace指令替换content内容。 6. URL处理 Thymeleaf可以处理URL,例如: ``` <a th:href="@{http://www.example.com}">Example Website</a> ``` 上面的代码中,将链接指向http://www.example.com。 以上是Thymeleaf的语法大全,希望能帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值