在 th:href 链接中使用模型属性 [英] Using model attributes in a th:href link
问题描述
有没有办法在 th:href 链接中引用模型属性?例如
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>
这里,currentUser 是控制器中指定的模型变量.正如在 th:text 标签中看到的那样,这可以很容易地访问.但是, th:href 无法解析百里香叶.如果有任何方式以这种方式引用模型属性,在 th:href 中?作为参考,这是一个支持 Thymeleaf 的 Spring MVC(引导程序)应用程序.
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.
推荐答案
为了在 th:href
中包含模型变量,您必须使用 ${...}
指示器,您可以使用管道轻松连接:
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>
关于 url 语法的官方文档此处.
Official documentation on url syntax here.
这篇关于在 th:href 链接中使用模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!