Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using model attributes in a th:href link

Is there a way of referencing a model attribute in a th:href link? For example

<a th:text="${currentUser}" th:href="@{/user/{currentUser}}"></a>

Here, currentUser is a model variable as specified in a controller. This can be accessed easily as seen in the th:text tag. However, the th:href fails the thymeleaf parsing. If there any way of referencing a model attribute in this way, in the th:href? For reference, this is a Spring MVC (bootstrap) application with Thymeleaf support.

like image 585
George Harris Avatar asked Mar 16 '15 19:03

George Harris


1 Answers

In order to include a model variable in th:href you have to include it with the ${...} indicator, you could use the pipes to easily concatenate:

<a th:text="${currentUser}" th:href="@{|/user/${currentUser}|}"></a>

Official documentation on url syntax here.

like image 84
Leandro Carracedo Avatar answered Oct 15 '22 14:10

Leandro Carracedo