Concating link with an expression

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Concating link with an expression

Reza
I am new to Thymeleaf
I need to create links like /item/the-best-book-ever/133 which only item is static the next part is the name of a book and then id of that book is 133. I don't know how to do this as I get syntax error.
Reply | Threaded
Open this post in threaded view
|

Re: Concating link with an expression

Zemi
Administrator
There are several options, but this is the simplest one:

    < a th:href="@{/item/__${book.name}__/__${book.id}__}" >

You could create an attribute processor in order to make syntax more concise.
Reply | Threaded
Open this post in threaded view
|

Re: Concating link with an expression

Reza
Thank you very much for your prompt and precise reply. :)
Reply | Threaded
Open this post in threaded view
|

Re: Concating link with an expression

danielfernandez
Administrator
In reply to this post by Zemi
Zemi wrote
    < a th:href="@{/item/__${book.name}__/__${book.id}__}" >
That's perfect, Zemi. Just to give Reza a different possibility, remember that you can use "variable expressions" (this is, OGNL or Spring EL, depending on the dialect you are using) as the base for your URLs, and so you could avoid preprocessing by doing something like:

    < a th:href="@{${'/item/' + book.name + '/' + book.id}}" >

Regards,
Daniel.


Reply | Threaded
Open this post in threaded view
|

Re: Concating link with an expression

xuxj
data-target="${id}" is not right,how should i do
jan
Reply | Threaded
Open this post in threaded view
|

Re: Concating link with an expression

jan
use
th:attr="data-target=${id}"
Reply | Threaded
Open this post in threaded view
|

Re: Concating link with an expression

xuxj
Thank you very much for your prompt and precise reply.