【发布时间】:2019-02-28 20:16:36
【问题描述】:
这些是我的简单 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";
}
还有,
<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>
我是 HTML 和 Spring 的初学者。如何通过th:href 标签将 post.id 值放入视图 mvc 控制器?
【问题讨论】:
标签: spring spring-boot thymeleaf