th:include thymeleaf 2.3.0 引入碎片并传入参数
时间: 2024-10-08 20:12:23 浏览: 44
`th:include` 是Thymeleaf模板引擎中的一个指令,用于引入其他HTML片段或模板文件,通常被称为“碎片”(Fragment)。在Thymeleaf 2.3.0版本及以后,你可以通过这个标签动态地插入内容到当前页面中,这在构建复杂的Web应用时非常有用,可以保持代码结构清晰。
使用`th:include` 的基本语法如下:
```html
<th:block th:include="partialFileName::partialTemplateName [param1='value1', param2='value2']"></th:block>
```
这里:
- `partialFileName` 是你要包含的碎片文件的相对路径(相对于模板文件的位置)。
- `partialTemplateName` 是你在碎片文件中定义的命名空间和片段名,`::` 用于分割。
- `[param1='value1', param2='value2']` 是传递给碎片的参数,它们作为key-value对的形式提供,并可以在接收端通过`th:with`指令获取。
例如,假设你有一个名为`shared/_footer.html`的碎片,你可以在主模板中这样引用它并传递日期参数:
```html
<th:block th:include="'shared/footer' :: footer (currentDate='#{now}')"></th:block>
```
在`_footer.html`中,你可以使用如下的方式获取并显示这个参数:
```html
<footer th:if="${currentDate}">
<p>Today's date is: <span th:text="${currentDate}"></span></p>
</footer>
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)