Amadeu Cabanilles
Amadeu Cabanilles

Reputation: 963

Thymeleaf. How to add a variable in the href

I have this Thymeleaf template , where I want to add the guardianId in the href element, to generate a URL like http://localhost:8080/guardian/delete/8665 but I got this instead http://localhost:8080/guardian/delete/$%7Bguardian.id%7D

<tr th:each="guardian : ${guardians}">

                                        <td class="col_id"      th:text="${guardian.id}"  ></td><!-- ID -->
                                        <td class="col_name"    th:text="${guardian.name}"></td><!-- NAME -->
                                        <td class="col_name"    th:text="${guardian.surName}"></td><!-- SURNAME -->
                                        <td class="col_name"    th:text="${guardian.description}"></td><!-- DESCRIPTION -->
                                        <td class="col_name"    th:text="${guardian.mobile}"></td><!-- MOBILE -->
                                        <td class="col_name"    th:text="${guardian.email}"></td><!-- EMAIL -->
                                        <td class="col_name"    >
                                                <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                        </td><!-- EDIT  -->
                                        <td class="col_name" >
                                            <a href="/guardian/delete/${guardian.id}" >
                                                <i class="fa fa-times" aria-hidden="true"></i>
                                            </a>
                                        </td><!-- DELETE -->

                                    </tr>

I also tried <a href="@{/guardian/delete/${guardian.id}}" > with the same result :-(

Upvotes: 1

Views: 12815

Answers (4)

Even the below will work. Basically constant string is separated out from variable using single quotes

<a th:href="@{'/guardian/delete/'+${guardian.id}}>

Upvotes: -1

RK Shrestha
RK Shrestha

Reputation: 159

I had same issues. I have googled it and found the solution.
You can use:

a th:href="@{/guardian/delete/{id}(id=${guardian.id})}

Upvotes: 1

Metroids
Metroids

Reputation: 20477

You probably want something like this:

<a th:href="@{~/guardian/delete/{id}(id=${guardian.id})}"></a>

But it really depends on your context, and what the url you want to generate. If your context already has /guardian in it, for example, you should use

<a th:href="@{/delete/{id}(id=${guardian.id})}"></a>

Upvotes: 2

FarZzTM
FarZzTM

Reputation: 115

I don't know Thymeleaf, but I think you can find a solution on this page.

Thymeleaf - Link (URL) expressions

Try like this.

<a href="@{/guardian/delete/${guardian.id}}" >

Upvotes: 1

Related Questions