项目问题合集<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thyme

 

首先在pom.xml引入thymeleaf的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


将上述的重复信息抽取出来存为pagination.html

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <body>
	<div class="panelBar" th:fragment="pagination">
           <!--以下为公共部分-->
           <div class="pages">
			<span>显示</span>
			<select class="combox" name="numPerPage" onchange="navTabPageBreak({numPerPage:this.value})">
				<option value="1" th:selected="${pages.numPerPage}==1">1</option>
				<option value="3" th:selected="${pages.numPerPage}==3">3</option>
				<option value="5" th:selected="${pages.numPerPage}==5">5</option>
				<option value="10" th:selected="${pages.numPerPage}==10">10</option>
				<option value="100" th:selected="${pages.numPerPage}==100">100</option>
				<option value="150" th:selected="${pages.numPerPage}==150">150</option>
				<option value="200" th:selected="${pages.numPerPage}==200">200</option>
				<option value="250" th:selected="${pages.numPerPage}==250">250</option>
			</select>
			<span id="fleeceRecordCounts" th:text="'共有'+${pages.totalCount}+'条'"></span>
		</div>
		<div id="fleece_page" class="pagination"  th:attr="targetType=${pages.targetType},totalCount=${pages.totalCount},numPerPage=${pages.numPerPage},pageNumShown=${pages.pageNumShown},currentPage=${pages.currentPage}"></div>
	</div>
    </body>
</html>

在其他页面进行引用该公共模块时如下:

<div class="panelBar" th:replace="pagination::pagination"></div>

注意:第一个pagination为上述公共部分的文件名,第二个pagination为th:fragment的值。这样便可以解决公共部分代码的抽取。

fragment加载语法如下:

  1. templatename::selector:”::”前面是模板文件名,后面是选择器
  2. ::selector:只写选择器,这里指fragment名称,则加载本页面对应的fragment
  3. templatename:只写模板文件名,则加载整个页面
<!--  语法说明  "::"前面是模板文件名,后面是选择器 -->
<div th:include="template/footer::copy"></div>
<!-- 只写选择器,这里指fragment名称,则加载本页面对应的fragment -->
<div th:include="::#thispage"></div>
<!-- 只写模板文件名,则加载整个页面 -->
<div th:include="template/footer"></div>

<span id="thispage">
    div in this page.
</span>

th:include 和 th:replace都是加载代码块内容,但是还是有所不同

  • th:include:加载模板的内容: 读取加载节点的内容(不含节点名称),替换div内容
  • th:replace:替换当前标签为模板中的标签,加载的节点会整个替换掉加载他的div 

公共部分如下:

<!-- th:fragment 定义用于加载的块 -->
<span th:fragment="pagination"> 
    the public pagination
</span>

调用

<!-- 加载模板的内容: 读取加载节点的内容(不含节点名称),替换<div>的内容 -->
<div th:include="pagination::pagination">1</div>
<!-- 替换当前标签为模板中的标签: 加载的节点会整个替换掉加载他的<div>  -->
<div th:replace="pagination::pagination">2</div>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现该功能的示例代码: HTML部分: ```html <!DOCTYPE html> <html> <head> <title>Interactive Bookstore</title> </head> <body> <select id="category"> <option value="">Select a category</option> <option value="children">Children</option> <option value="cooking">Cooking</option> <option value="web">Web</option> </select> <table id="bookTable"> <thead> <tr> <th>Author</th> <th>Year</th> <th>Price</th> </tr> </thead> <tbody> </tbody> </table> <script src="script.js"></script> </body> </html> ``` JavaScript部分: ```javascript // 加载xml文件 var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { displayBooks(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); // 处理选择器变化事件 document.getElementById("category").addEventListener("change", function() { var selectedCategory = this.value; var books = document.getElementsByTagName("book"); // 遍历所有书籍,根据类别显示或隐藏 for (var i = 0; i < books.length; i++) { if (selectedCategory == "" || books[i].getAttribute("category") == selectedCategory) { books[i].style.display = "table-row"; } else { books[i].style.display = "none"; } } }); // 显示所有书籍 function displayBooks(xml) { var xmlDoc = xml.responseXML; var books = xmlDoc.getElementsByTagName("book"); var bookTable = document.getElementById("bookTable"); // 遍历所有书籍,并在表格中添加新行 for (var i = 0; i < books.length; i++) { var row = bookTable.insertRow(-1); row.setAttribute("category", books[i].getAttribute("category")); row.style.display = "table-row"; var authorCell = row.insertCell(0); var yearCell = row.insertCell(1); var priceCell = row.insertCell(2); authorCell.innerHTML = books[i].getElementsByTagName("author")[0].childNodes[0].nodeValue; yearCell.innerHTML = books[i].getElementsByTagName("year")[0].childNodes[0].nodeValue; priceCell.innerHTML = books[i].getElementsByTagName("price")[0].childNodes[0].nodeValue; } } ``` 在该示例中,我们首先使用XMLHttpRequest对象加载books.xml文件。一旦文件加载完成,我们调用displayBooks函数来解析XML并在表格中显示所有书籍信息。 我们还在页面上添加了一个下拉单,使用户可以选择要显示的书籍类别。在选择器变化事件中,我们遍历所有书籍,并根据类别显示或隐藏它们。 在表格中显示书籍信息时,我们使用insertRow函数向表格添加新行,并使用insertCell函数向行添加新单元格。我们还使用getAttribute和getElementsByTagName函数从XML中获取书籍的类别、作者、年份和价格信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值