问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/29084927/using-model-attributes-in-a-thhref-link