thymeleaf模版实现页面布局(layout)两种方法
来源:互联网 发布:庆钰软件科技有限公司 编辑:程序博客网 时间:2024/05/16 14:24
方法一:
使用thymeleaf的layout:fragment标签,布局页如下:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> * {font-family: Microsoft YaHei, Tahoma, Helvetica, Arial, sans-serif;} .header {background-color: #f5f5f5;padding: 20px;} .header a {padding: 0 20px;} .container {padding: 20px;margin:20px auto;} .footer {height: 40px;background-color: #f5f5f5;border-top: 1px solid #ddd;padding: 20px;} </style></head><body> <header class="header"> <div> <a th:href="@{/one}">布局方法一</a> <a th:href="@{/two}">布局方法二</a> </div> </header> <div class="container" layout:fragment="content"></div> <footer class="footer"> <div> <p style="float: left">© Hylun 2017</p> <p style="float: right"> Powered by <a href="http://my.oschina.net/alun" target="_blank">Alun</a> </p> </div> </footer> <script th:inline="javascript" th:if="${alertMsg}">alert('[[${alertMsg}]]');</script></body></html>
内容页如下:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="layout1"><!-- layout文件路径--><head> <title>页面1</title></head><body> <div layout:fragment="content"> 页1内容 页1内容 页1内容 页1内容 页1内容 页1内容 页1内容 页1内容 页1内容 页1内容 </div></body></html>
在页面渲染时,thymeleaf引擎会将内容页中
中的内容替换至布局页的,达到布局的目的。方法二:
使用thymeleaf的th:include或th:replace标签,布局页如下:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <title th:text="${title}"></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> * {font-family: Microsoft YaHei, Tahoma, Helvetica, Arial, sans-serif;} .header {background-color: #f5f5f5;padding: 20px;} .header a {padding: 0 20px;} .container {padding: 20px;margin:20px auto;} .footer {height: 40px;background-color: #f5f5f5;border-top: 1px solid #ddd;padding: 20px;} </style></head><body> <header class="header"> <div> <a th:href="@{/one}">布局方法一</a> <a th:href="@{/two}">布局方法二</a> </div> </header> <div class="container" th:include="::content"></div> <footer class="footer"> <div> <p style="float: left">© Hylun 2017</p> <p style="float: right"> Powered by <a href="http://my.oschina.net/alun" target="_blank">Alun</a> </p> </div> </footer> <script th:inline="javascript" th:if="${alertMsg}">alert('[[${alertMsg}]]');</script></body></html>
内容页如下:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org" th:replace="layout2 (title='页面2')"><!-- layout文件路径--> <div th:fragment="content"> 页2内容 页2内容 页2内容 页2内容 页2内容 </div></html>
在一般博文中多为使用方法一,方法二也一般只用于包含单个页面片段,其实th:include及th:replace包含整个页面也是没有问题的,而且还可以在包含的同时传参数给包含的页面,强烈推荐使用此方法
用springboot编写的示例demo已经更新至https://github.com/hylun/SpringWeb/tree/master/springboot-thymeleaf-layout-demo 或
https://gitee.com/hylun/SpringWeb/tree/master/springboot-thymeleaf-layout-demo
欢迎围观!
阅读全文
0 0
- thymeleaf模版实现页面布局(layout)两种方法
- Thymeleaf利用layout.html文件生成页面布局框架
- layout+ifram实现菜单页面布局
- thymeleaf中fragment 的layout布局
- 页面布局(Page Layout )
- 页面布局:Layout
- android Layout布局转换成View对象的两种方法,如图所示
- thymeleaf中layout:fragment的使用和布局
- DIV+CSS页面布局模版
- Android实现BaseAdapter布局的两种方法
- 实现两列等宽布局的几种方法
- [yii2]不加载layout布局文件的实现方法
- 页面布局:两列布局
- 两种页面布局(Tiles、Sitemesh)
- DIV与Table两种页面布局
- PHP生成静态页面的两种实现方法
- 网页里实现页面折叠的两种方法
- servlet 实现页面跳转的两种方法
- 51Nod 1459 迷宫游戏
- java 基于poi 写入excel 合并单元格
- LoRa技术如何使供应链和资产管理变得更智能
- DevicePass-through及网卡的直接分配在Xen里面的实现
- 在linux上配置Maven环境变量(重启mvn -v无效的解决方法)
- thymeleaf模版实现页面布局(layout)两种方法
- 浅谈数据库主从复制
- Jodd利器--开源Java微框架和工具
- 新手村 过程函数与递归 回文质数
- 2007年分区联赛提高组之一 统计数字
- hidapi在linux下编译总结
- 房钊的博客
- mysql下this is incompatible with sql_mode=only_full_group_by解决方案
- linux cp命令直接覆盖不提示按Y/N的方法