Thymeleaf using path variables to th:href
Asked Answered
O

12

58

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)

Octosyllable answered 17/11, 2015 at 9:59 Comment(3)
Are u sure that id is a property of a category object? Or is idCategory semantically different?Flatt
what different between idCategory and id?Closure
Do you have the rest of the stack trace?Mycah
A
121

The right way according to Thymeleaf documention for adding parameters is:

<a th:href="@{/category/edit/{id}(id=${category.idCategory})}">view</a>
Ascham answered 12/3, 2018 at 1:12 Comment(1)
That's a very underappreciated answer . To my opinion that's the best one.Igniter
B
62

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>
Backsheesh answered 26/3, 2016 at 12:54 Comment(0)
P
29

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".

Priebe answered 23/1, 2018 at 16:32 Comment(1)
Very nice. I hope people see this answer - this is far nicer and more compact than the other approaches listed here, avoiding the need for '...' or duplicating the names with the the (...) link syntaxCarpel
P
14

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>
Polio answered 16/1, 2016 at 9:22 Comment(0)
L
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>
Lexie answered 7/6, 2018 at 21:50 Comment(0)
A
5

You can use like

  1. My table is bellow like..

    <table>
       <thead>
        <tr>
            <th>Details</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user: ${staffList}">
            <td><a th:href="@{'/details-view/'+ ${user.userId}}">Details</a></td>
        </tr>
     </tbody>
    </table>
    
  2. Here is my controller ..

    @GetMapping(value = "/details-view/{userId}")
    public String details(@PathVariable String userId) { 
    
        Logger.getLogger(getClass().getName()).info("userId-->" + userId);
    
     return "user-details";
    }
    
Austine answered 12/5, 2020 at 5:39 Comment(1)
This thing worked for me in this case: <a th:href="${'localhost:8080/reset-password?otp='} + ${otp}">Click Here to reset your Password.</a>Gaskell
D
4

I think you can try this:

<a th:href="${'/category/edit/' + {category.id}}">view</a>

Or if you have "idCategory" this:

<a th:href="${'/category/edit/' + {category.idCategory}}">view</a>
Dialectician answered 2/5, 2016 at 12:2 Comment(0)
B
3

You can use:

html:

 <html xmlns:th="http://www.thymeleaf.org">

<a th:href="@{'/category/'+ ${item.idCategory}}">View</i></a>

Controller: @PathVariable String parentId

Bonnice answered 19/5, 2021 at 4:7 Comment(1)
<a th:href="${'/category/edit/' + {category.idCategory}}">view</a>Captivate
L
2

"List" is an object getting from backend and using iterator to display in table

"minAmount" , "MaxAmount" is an object variable "mrr" is an just temporary var to get value and iterate mrr to get data.

<table class="table table-hover">
<tbody>
<tr th:each="mrr,iterStat : ${list}">
        <td th:text="${mrr.id}"></td>
        <td th:text="${mrr.minAmount}"></td>
        <td th:text="${mrr.maxAmount}"></td>
</tr>
</tbody>
</table>
Lauranlaurance answered 24/6, 2018 at 15:23 Comment(0)
P
0

try this one is a very easy method if you are in list of model foreach (var item in Modal) loop

<th:href="/category/edit/@item.idCategory>View</a>"

or

<th:href="/category/edit/@item.idCategory">View</a>

Pheni answered 13/7, 2020 at 8:14 Comment(0)
L
0

You can also use ${yourVariable} for exampe <th:href="/category/edit/__${yourVariable}__">View</a>

Luba answered 31/10, 2022 at 1:17 Comment(0)
G
0

Hi you can use thymeleaf preprocessing :

<tr th:each="category : ${categories}">
    <td th:text="${category.idCategory}"></td>
    <td th:text="${category.name}"></td>
    <td>
        <a th:href="@{__${category.idCategory}__}">view</a>
    </td>
 </tr>
Griz answered 18/2, 2023 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.