html - 如何将变量值附加到 th :href in thymeleaf

标签 html thymeleaf

当我在前端点击链接(brandname)时,我需要使用th:href向服务器发送请求。在 thymeleaf 中。为此,我遵循了以下代码

<a th:href="@{*{category.url}}" th:text=*{brandname}></a>

category.urlbrandname值动态地来自它们各自的 Objects , 但对于上述请求,我需要附加额外的内容,我尝试了如下内容

<a th:href="@{*{category.url}+'?someExtraContent'}" th:text=*{brandname}></a>

它成功添加了额外的内容,现在我的任务是我需要动态附加的“额外内容”,我阅读了 thymeleaf 中的局部变量概念 并尝试使用下面的代码将内容附加到动态请求url

<a th:with="var=*{brandname}" th:href="@{*{category.url}+'?'+var}" th:text=*{brandname}></a>

我假设 var将存储该值并将其附加到请求 url 但它没有发生我得到的 url 如下所示

例如: localhost:8080/mycompany/mens?var

但是我需要像下面这样的东西

例如: localhost:8080/mycompany/mens?nike

任何人都可以帮助我如何将动态内容附加到 thymeleaf 中的请求 url

最佳答案

我找到了解决方案,

上一个代码

<a th:with="var=*{brandname}" th:href="@{*{category.url}+'?'+var}" th:text=*{brandname}></a>

我将上面的代码替换为

<a th:with="var=*{brandname}" th:href="@{*{category.url}+'?'+${var}}" th:text=*{brandname}></a>

现在我可以将动态内容附加到我的请求 url 中...

我已经改变了这个var${var}

关于html - 如何将变量值附加到 th :href in thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37069322/

相关文章:

javascript - jQuery 隐藏不同表上的列

html - 2个相邻的div进入下一个div

javascript - 用于 JS 的 Eclipse 2020 语言服务器在 thymeleaf 上向 javascript 变量显示错误

java - 使用 Spring boot 在 thymeleaf 模板中打印数据时出现问题

java - Flying Saucer/OpenPdf相同的字符串html内容但不同的字节[]

java - 具有一对多关系的thymeleaf表单字段的设置值

javascript - 将等待光标显示为页面元素

html - 启用 CSS 选项卡中的内容

html - IE7 float UL 不显示

java - 如何使用 Thymeleaf 通过 "ID"选择列表项并定向到另一个详细信息页面?