Thymeleaf using path variables to th:href
Solution 1
The right way according to Thymeleaf documention for adding parameters is:
<a th:href="@{/category/edit/{id}(id=${category.idCategory})}">view</a>
Solution 2
I think your problem was a typo:
<a th:href="@{'/category/edit/' + ${category.id}}">view</a>
You are using category.id
, but in your code is idCategory
, as Eddie already pointed out.
This would work for you:
<a th:href="@{'/category/edit/' + ${category.idCategory}}">view</a>
Solution 3
A cleaner and easier way to do this
<a href="somepage.html" th:href="@{|/my/url/${variable}|}">A Link</a>
I found this solution in Thymeleaf Documentation on "4.8 Literal substitutions".
Solution 4
Your code looks syntactically correct, but I think your property doesn't exist to create the URL.
I just tested it, and it works fine for me.
Try using category.idCategory
instead of category.id
, for example…
<tr th:each="category : ${categories}">
<td th:text="${category.idCategory}"></td>
<td th:text="${category.name}"></td>
<td>
<a th:href="@{'/category/edit/' + ${category.idCategory}}">view</a>
</td>
</tr>
Solution 5
I was trying to go through a list of objects, display them as rows in a table, with each row being a link. This worked for me. Hope it helps.
// CUSTOMER_LIST is a model attribute
<table>
<th:block th:each="customer : ${CUSTOMER_LIST}">
<tr>
<td><a th:href="@{'/main?id=' + ${customer.id}}" th:text="${customer.fullName}" /></td>
</tr>
</th:block>
</table>
Related videos on Youtube
user962206
Updated on July 09, 2022Comments
-
user962206 almost 2 years
Here's my code, where I'm iterating through:
<tr th:each="category : ${categories}"> <td th:text="${category.idCategory}"></td> <td th:text="${category.name}"></td> <td> <a th:href="@{'/category/edit/' + ${category.id}}">view</a> </td> </tr>
The URL it points to is supposed to be
/category/edit/<id of the category>
, but it says it could not parse the expression:Exception evaluating SpringEL expression: "category.id" (category-list:21)
-
Julian L. over 8 yearsAre u sure that
id
is a property of a category object? Or isidCategory
semantically different? -
codeaholicguy over 8 yearswhat different between idCategory and id?
-
Andrew over 8 yearsDo you have the rest of the stack trace?
-
-
MarcDeXeT about 5 yearsThat's a very underappreciated answer . To my opinion that's the best one.
-
Lâm Hồ almost 3 years<a th:href="${'/category/edit/' + {category.idCategory}}">view</a>
-
Software Engineer almost 3 yearsThis thing worked for me in this case: <a th:href="${'localhost:8080/reset-password?otp='} + ${otp}">Click Here to reset your Password.</a>