Using model attributes in a th:href link

我的未来我决定 提交于 2021-02-07 14:52:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!