【问题标题】:Thymeleaf/SB How do I pass in a variable from an object to a link element's href attribute?Thymeleaf/SB 如何将变量从对象传递到链接元素的 href 属性?
【发布时间】:2021-10-03 14:34:12
【问题描述】:

我想将 project.url 变量传递给元素的 th:href 属性,但目前导航到的 URL 是 - /project/project.url/contribute 这显然不是我想要发生的. 按钮上方的列表显示了项目对象的信息,并且 URL 显示正确。

    <ul>

    <span>Title: </span>
    <li th:text="${project.url}">Title</li>

    <span>Started: </span>
    <li th:text="${project.publishedOn}">Started</li>

    <span>Target: </span>
    <li th:text="${project.targets}">Target</li>

    <span>Description: </span>
    <li th:text="${project.description}">Description</li>

    <span>Amount Contributed: </span>
    <li th:text="${project.amountContributed}">Amount Contributed</li>

</ul>

<div class="contributeButton">
    <button>
 <!-- <a href="../project/' + ${project.url} + '/contribute">Contribute</a> -->
 <a th:href="@{/project/${project.url}/contribute(action='show_all')}">Contribute</a>
    </button>
</div>

我需要在链接中添加什么 href 才能正常工作?谢谢。

【问题讨论】:

    标签: spring spring-boot spring-mvc templates thymeleaf


    【解决方案1】:

    您可以使用串联来执行此操作。我还没有测试过,但试一试:

    <a th:href="@{'/project/' + ${project.url} + '/contribute(action=\'show_all\')'}">view</a>
    

    【讨论】:

    • 谢谢!我不得不删除字符串的 (action) 部分,但无论如何我都不需要。
    【解决方案2】:

    您应该为此利用 Thymeleaf 的 standard URL syntax

    <a th:href="@{/project/{url}/contribute(url=${project.url})}">view</a>
    

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      相关资源
      最近更新 更多