devo9191
devo9191

Reputation: 219

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.

Upvotes: 0

Views: 1057

Answers (2)

Metroids
Metroids

Reputation: 20477

You should be taking advantage of Thymeleaf's standard URL syntax for this:

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

Upvotes: 1

Lee Greiner
Lee Greiner

Reputation: 1082

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>

Upvotes: 1

Related Questions