Question

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

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

This produces the following (where lid=45):

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

So, it did the substitution on the th:text, but not on the th:href.

I'm not trying to do any sort of URL re-writing, I'm just using the '@' syntax because I want Thymeleaf to substitute the 'lid' attribute.

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

Was it helpful?

Solution

If you don't want to do any url rewriting, you shouldn't use the @ syntax.

You can use the pipeline (|) syntax to do some literal substitions:

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

Source: Thymeleaf documentation

OTHER TIPS

You can also try this way:

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

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

Thymeleaf Standard URL Syntax

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

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

Will produce a link similar to:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top