Timeleaf - combined: each with th: href

I'm new to Thymeleaf (and webdev), and I'm trying to combine the iteration of Thymeleaf (th: each) with re-writing the URL (th: href).

<a th:each="lid : ${lists}" th:text="${lid}" th:href="@{/list?l=${lid}}">
hello
</a>

The result is the following (where lid = 45):

<a href="/list?l=${lid}">45</a>

So, he replaced the text th: text, but not with th: href.

I am not trying to rewrite the URLs, I just use the “@” syntax because I want Thymeleaf to replace the “lid” attribute.

I am using the current version of Thymeleaf (2.1.2) with the Google App Engine.

+3
source share
3 answers

If you do not want to rewrite the URL, you should not use syntax @.

You can use the syntax of the pipeline ( |) to perform some literary permutations:

th:href="|/list?l=${lid}|"

: Thymeleaf

+6

:

<a th:href="@{'/list?l=' + ${lid}}" th:text="${lid}">element</a>
+2

I don't have enough reputation to add a comment to a previous post, but the link to the original Thymeleaf Source documentation from the previous post is broken. Now the documentation can be found at the following link:

Typical Timeleaf Syntax

Section 9 Using Expressions in URLs in this documentation explains how you can use expressions in other expressions when generating URLs with the @ syntax. The following is an example:

<a th:href="@{/order/details(id=${modelattribute})}"/>

Will create a link similar to:

http://domain.org/context/order/details?id=1

if the modelattribute parameter had a value of 1 in the current context.

0
source

All Articles