Thymeleaf using path variables to th:href

139,358

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>
Share:
139,358

Related videos on Youtube

user962206
Author by

user962206

Updated on July 09, 2022

Comments

  • user962206
    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.
      Julian L. over 8 years
      Are u sure that id is a property of a category object? Or is idCategory semantically different?
    • codeaholicguy
      codeaholicguy over 8 years
      what different between idCategory and id?
    • Andrew
      Andrew over 8 years
      Do you have the rest of the stack trace?
  • MarcDeXeT
    MarcDeXeT about 5 years
    That's a very underappreciated answer . To my opinion that's the best one.
  • Lâm Hồ
    Lâm Hồ almost 3 years
    <a th:href="${'/category/edit/' + {category.idCategory}}">view</a>
  • Software Engineer
    Software Engineer almost 3 years
    This thing worked for me in this case: <a th:href="${'localhost:8080/reset-password?otp='} + ${otp}">Click Here to reset your Password.</a>