feshout
feshout

Reputation: 92

Thymeleaf how to link th:href in iteration

Hello i would like to link all of list elements with specific id. What should i change in my code? Context is added properly, but i can generate site from this template

My code looks like this:

<tr th:each="mentor : ${mentorsList}">
    <a th:href="@{'/edit-mentor/' + ${mentor.id}}"/>
        <td th:text="${mentorStat.count}">1</td>
        <td th:text="${mentor.name}">Adam</td>
        <td th:text="${mentor.surname}">Nowak</td>
        <td th:text="${mentor.email}">[email protected]</td>
    </a>
</tr>

After redirection i would like to get link like /edit-mentor/32

Upvotes: 1

Views: 4011

Answers (2)

MohamedSanaulla
MohamedSanaulla

Reputation: 6252

Interesting question, I dont understand why the code which you shared didn't work. You didn't tell us the error as well. In anycase here is a much cleaner approach to create links:

<a th:href="@{|/edit-mentor/${mentor.id}|}"></a>

Also your <a> tag is getting closed twice, i.e you have already closed it using this <a /> and then you are trying to close again using </a>. If you are using version before Thymeleaf 3, then it will give you an error, but Thymeleaf 3 will silently try to fix it for you and sometimes leading to weird UI behavior.

Upvotes: 0

Jigar Shah
Jigar Shah

Reputation: 470

You can make the following changes to your code

<tr th:each="mentor : ${mentorsList}">
    <a th:href="${'/edit-mentor/' + mentor.id}"/>
        <td th:text="${mentorStat.count}">1</td>
        <td th:text="${mentor.name}">Adam</td>
        <td th:text="${mentor.surname}">Nowak</td>
        <td th:text="${mentor.email}">[email protected]</td>
    </a>
</tr>

This should do your work

Upvotes: 3

Related Questions