> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/programming-language/java/web-bian-cheng/thymeleaf.md).

# Thymeleaf

fragment 类似jsp的tag，可以用于抽取重复代码模板，定义组件，并且可以朝fragment中传递参数，较为灵活。

参考原文： <https://blog.csdn.net/believe__sss/article/details/79992408>

定义模块， 在component目录下定义pagination.html 作为组件模板，用作公共代码抽取：

```html
<!-- th:fragment 定义用于加载的块 -->
<!DOCTYPE html "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>
<span th:fragment="pagination">
the public pagination
</span>
</body>
</html>

```

在根路径下创建 index.html，引用上述模块文件：

```html
================= th:include 和 th:replace============================
<!-- 加载模板的内容： 读取加载节点的内容（不含节点名称），替换<div>的内容 -->
<div th:include="component/pagination::pagination">1</div>
<!-- 替换当前标签为模板中的标签： 加载的节点会整个替换掉加载他的<div>  -->
<div th:replace="component/pagination::pagination">2</div>
```

执行结果：

```html
<!-- 加载模板的内容： 读取加载节点的内容（不含节点名称），替换<div>的内容 -->
<div> the public pagination</div>
<!-- 替换当前标签为模板中的标签： 加载的节点会整个替换掉加载他的<div>  -->
<span> the public pagination</span>
```

* `th:include`：加载模板的内容： 读取加载节点的内容（不含节点名称），替换div内容
* `th:replace`：替换当前标签为模板中的标签，加载的节点会整个替换掉加载他的div


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsx95.gitbook.io/notes/programming-language/java/web-bian-cheng/thymeleaf.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
