Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf using path variables to th:href

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)

like image 373
user962206 Avatar asked Nov 17 '15 09:11

user962206


People also ask

How do you pass parameters in Thymeleaf?

Request parameters can be easily accessed in Thymeleaf views. Request parameters are passed from the client to server like: https://example.com/query?q=Thymeleaf+Is+Great! In the above example if parameter q is not present, empty string will be displayed in the above paragraph otherwise the value of q will be shown.

How do you set a variable in Thymeleaf?

We can use the th:with attribute to declare local variables in Thymeleaf templates. A local variable in Thymeleaf is only available for evaluation on all children inside the bounds of the HTML tag that declares it.

How do I use the Thymeleaf th object?

We use th:action to provide the form action URL and th:object to specify an object to which the submitted form data will be bound. Individual fields are mapped using the th:field=”*{name}” attribute, where the name is the matching property of the object.


1 Answers

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

<a th:href="@{/category/edit/{id}(id=${category.idCategory})}">view</a> 
like image 178
Mario Rojas Avatar answered Sep 17 '22 14:09

Mario Rojas