Reputation: 1421
These are my simple Thymeleaf table HTML file and Spring MVC controller codes. First below is my table image.
I try to make some html codes to transfer the post id value to view codes when the 'Edit' or 'Delete' link are clicked, but I have no idea how to do. These are my Spring MVC Controller codes and view.html codes.
@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";
}
And,
<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>
I am a beginner in HTML and Spring. How can I put post.id value into view mvc controller through th:href
tag?
Upvotes: 4
Views: 11030
Reputation: 125
You can add parameters in parentheses:
<a th:href="@{/index(param1='value1',param2='value2')}">
Thymeleaf evaluates the above to:
<a href="/index?param1=value1,m2=value2">
For example, suppose we set default value to two parameters; name="Dracula" and age = 25.
<a th:href=@{/person(name=${name},age=${age})}></a>
<a href="/person?name='Dracula',age='25'"></a>
Upvotes: 3
Reputation: 11
I used in this format and work fine.
<a th:href="@{${urlBase} + '/#!/pedido' + ${other.value}" target="_blank">Link</a>
Upvotes: 1
Reputation: 1545
Use th:href like described in the documentation
- th:href is an attribute modifier attribute: once processed, it will compute the link URL to be used and set the href attribute of the tag to this URL.
- We are allowed to use expressions for URL parameters (as you can see in orderId=${o.id}). The required URL-encoding operations will also be automatically performed.
- If several parameters are needed, these will be separated by commas like @{/order/process(execId=${execId},execType='FAST')}
- Variable templates are also allowed in URL paths, like @{/order/{orderId}/details(orderId=${orderId})}
For example (notice the th:href
and the parameter postId
that receives the value from your variable post
):
<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>
Upvotes: 6
Reputation: 3305
You can something try like following:
<a href="posts/view.html" th:href="@{/posts/view/__${post.id}__}">Edit</a><br/>
<a href="posts/view.html" th:href="@{/posts/view/__${post.id}__}">Delete</a>
Upvotes: 0