Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer parameters in th:href in spring boot Thymeleaf?

These are my simple Thymeleaf table HTML file and Spring MVC controller codes. First below is my table image.

enter image description here

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?

like image 358
Joseph Hwang Avatar asked Jan 27 '23 09:01

Joseph Hwang


1 Answers

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>
like image 169
GabiM Avatar answered Jan 31 '23 07:01

GabiM