Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thymeleaf - combined th:each with th:href

Tags:

java

thymeleaf

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.

like image 570
Tom Avatar asked Feb 22 '14 23:02

Tom


2 Answers

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

like image 146
Tom Verelst Avatar answered Oct 10 '22 02:10

Tom Verelst


You can also try this way:

<a th:href="@{'/list?l=' + ${lid}}" th:text="${lid}">element</a>
like image 43
Iwo Kucharski Avatar answered Oct 10 '22 02:10

Iwo Kucharski