Jos*_*ang 4 spring thymeleaf spring-boot
这些是我的简单 Thymeleaf 表 HTML 文件和 Spring MVC 控制器代码。下面首先是我的表格图像。
我尝试制作一些 html 代码,以便在单击“编辑”或“删除”链接时将帖子 id 值传输到查看代码,但我不知道该怎么做。这些是我的 Spring MVC 控制器代码和 view.html 代码。
@Controller
public class PostController {
@Autowired
private PostService postService;
@RequestMapping("/posts/view/{id}")
public String view(@PathVariable("id") Long id, Model model) {
Post post = postService.findById(id);
model.addAttribute("post", post);
return "posts/view";
}
Run Code Online (Sandbox Code Playgroud)
和,
<table id="blogTable" border="1" width ="1000" height="400" align = "center">
<thead>
<tr>
<th>Post ID</th>
<th>Post Title</th>
<th>Post Content</th>
<th>Date</th>
<th>Author</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="post : ${posts}">
<td th:text="${post.id}">Post ID</td>
<td th:text="${post.title}">Post Title</td>
<td th:text="${post.body}">Post Content</td>
<td th:text="${post.date}">Date</td>
<!-- <td th:text="${post.auther.userName()}">Author</td> -->
<td>
<a href="posts/view.html" th:href="@{posts/view/post.id}">Edit</a><br/> ==> How to transfer the post.id parameter to th:href?
<a href="posts/view.html" th:href="@{posts/view/post.id}">Delete</a> ==> How to transfer the post.id parameter to th:href?
</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我是 HTML 和 Spring 的初学者。如何通过th:href标签将 post.id 值放入 view mvc 控制器中?
按照文档中的描述使用 th:href
- th:href 是一个属性修饰符属性:处理后,它将计算要使用的链接 URL,并将标签的 href 属性设置为该 URL。
- 我们可以使用 URL 参数的表达式(如 orderId=${o.id} 中所示)。所需的 URL 编码操作也将自动执行。
- 如果需要多个参数,则将用逗号分隔,例如 @{/order/process(execId=${execId},execType='FAST')}
- URL 路径中也允许使用变量模板,例如 @{/order/{orderId}/details(orderId=${orderId})}
例如(注意从变量接收值的th:href和 参数):postIdpost
<td>
<a href="posts/view.html" th:href="@{posts/view/{postId}(postId=${post.id})}">Edit</a><br/> ==> How to transfer the post.id parameter to th:href?
</td>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10442 次 |
| 最近记录: |