2.3.0 thymeleaf th:include 引入碎片 带参数
时间: 2024-10-08 22:11:38 浏览: 87
Thymeleaf是一个强大的模板引擎,特别适合于Spring MVC项目中使用。`th:include`标签用于在当前模板中引入另一个模板(也称为“碎片”或“partial view”),并且可以传递参数到被包含的模板。
在2.3.0版本的Thymeleaf中,使用`th:include`的基本语法如下:
```html
<th:block th:include="relative/path/to/fragment :: [optional]attributeName=value">
<!-- 这里放置要被包含的内容 -->
</th:block>
```
这里的参数是在`::`之后的部分,`attributeName`是你自定义的参数名,`value`则是你要传入的实际值。例如,如果你想在被包含的模板中使用名为`user`的对象,你可以这样做:
```html
<!-- 主模板 -->
<th:block th:include="fragments/user-profile :: user=${user}"></th:block>
<!-- fragment文件 (user-profile.html) -->
<div th:if="${user}">
<h2>User Profile</h2>
<p>Name: ${user.name}</p>
<p>Email: ${user.email}</p>
</div>
```
在这个例子中,`user-profile.html`会接收到`user`参数,并可以根据这个参数显示用户信息。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.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://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)