Thymeleaf模板使用实例+模板分页

本文介绍了Thymeleaf中如何使用th:fragment、th:include、th:replace和参数化模板配置进行页面复用。通过实例展示了如何引入公共头文件,并探讨了使用Vue.js进行分页的实现方式。详细讲述了如何查找和配置Thymeleaf模板路径,以及在实际项目中应用这些技巧。
摘要由CSDN通过智能技术生成

1. 概述

在web开发中,我们经常会将公共头,公共尾,菜单等部分提取成模板供其它页面使用。在thymeleaf中,通过th:fragment、th:include、th:replace、参数化模板配置、css选择器加载代码块等实现。下文通过例子来说明用法:

  • fragment语法
  • 通过 th:fragment 和 css选择器加载代码块
  • th:include 和 th:replace
  • 参数化模板配置

Thymeleaf基本搭建和配置可以参考其他网站的配置实例,这里不详细说明。

2.引入头文件

首先我们讲述web项目里的js,css如何放到一个公共页面,用thymeleaf模板语法调用呢?其实thymeleaf里有个标签可以使用:

initHead.html

<th:block id="initHead"><!-- 可以充当HTML标签,但在模板执行后该标签会消失 -->
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<script type="text/javascript" th:src="@{/resources/js/share/backstage/jquery.js}"></script>

<!-- jqueryUI -->
<script type="text/javascript" th:src="@{/resources/js/share/jquery-ui-1.12.1.custom/external/jquery/jquery.js}"></script>
<link th:href="@{/resources/js/share/jquery-ui-1.12.1.custom/jquery-ui.css}" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var rootPath = "/{项目名}";
</script>
</th:block>

homepage.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>主页</title>
<th:block th:replace="common/base/initHead :: #initHead"></th:block><!-- 调用模板 -->
<script language="javascript">
$(function() {});
</script>
</head>
<body class="homepage_body">....common/base/initHead :: #initHead"></th:block><!-- 调用模板 -->
<script language="javascript">
$(function() {});
</script>
</head>
<body class="homepage_body">....

问题来了,我们如何找到initHead.html文件呢?

其实也是通过配置的thymeleaf路径下查找。例如我配置了

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值