Search code examples
htmlspringjspspring-bootthymeleaf

Spring boot thymeleaf direction to id user


Could you please help me with one issue. I want go to current user by id but I don't know how write it by thymeleaf. In jsp I have wrote:

<c:forEach items="${users}" var="user">
<a href="/user-${user.id}">${user.username}</a><br>
</c:forEach>

By thymeleaf I tried do it in this way:

<th:block th:each="user:${users}">
<a th:href="@{/user}"><p th:text="${user.username}"></p></a>
</th:block>

Please help with this issue. Maybe this is not a reasonable question, but it would be great if you help me.

Thank you.


Solution

  • As per my understanding of the question you asked, you want to create a link with your user_id linked with it. For that you can try this :--

    <th:block th:each="user:${users}">
         <a th:href="@{user(id=${user.id})}"><p th:text="${user.username}"></p></a>
    </th:block>
    

    On your controller would be like this :--

        @RequestMapping("/user")
        public String editCategory(@RequestParam("id") String userId){
    
                //your code
    
        }