Search code examples
springspring-bootspring-mvctemplatesthymeleaf

Thymeleaf/SB How do I pass in a variable from an object to a link element's href attribute?


I want to pass in the project.url variable into the th:href attribute of the element, but currently the URL that is navigated to is - /project/project.url/contribute which is obviously not what I wanted to happen. The list above the button displays the project object's information and the URL is shown correctly.

    <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>

What href do I need to put inside the link for this to work correctly? Thanks.


Solution

  • You can use concatenation to do this. I haven't tested this but give it a try:

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