在th:href链接中使用模型属性

Geo*_*ris 5 html spring spring-mvc thymeleaf spring-boot

有没有办法在th:href链接中引用模型属性?例如

<a th:text="${currentUser}" th:href="@{/user/{currentUser}}"></a>
Run Code Online (Sandbox Code Playgroud)

这里,currentUser是控制器中指定的模型变量.这可以很容易地访问,如th:text标签中所示.但是,th:href无法解决百里香的问题.如果有任何方式以这种方式引用模型属性,在th:href?作为参考,这是一个支持Thymeleaf的Spring MVC(bootstrap)应用程序.

Lea*_*edo 10

为了包含模型变量,th:href你必须将它包含在${...}指标中,你可以使用管道轻松连接:

<a th:text="${currentUser}" th:href="@{|/user/${currentUser}|}"></a>
Run Code Online (Sandbox Code Playgroud)

在URL语法官方文档在这里.

  • 工作完美,谢谢帕特里克 - 不知道 Thymeleaf 以这种方式支持字符串连接 (2认同)