Search code examples
javahtmlthymeleaf

Thymeleaf download attribute


In my Thymeleaf template I am able to process Java-map content to html like this:

<div th:each="file : ${files}">
     <span th:text="${file.key}"></span> <!-- here file.key is readed -->
     ----
</div>

Then I try to add html download attribute that specifies the name of downloaded file:

<a th:href="..." th:download="${file.key}" > Download </a>

but the th:download attribute is not processed on the server, the generated html looks like this:

<a th:download="${file.key}" href="...ok..."> Download </a>

How could I get access to file.key property and add it to download-attribute?


Solution

  • If you want to set the value of arbitrary attributes (not just the ones Thymeleaf supports) then you can use th:attr. (Documentation). eg:

    <a th:href="..." th:attr="download=${file.key}" > Download </a>