Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping '&' character in thymeleaf

I need an image loaded onto a html img tag using thymeleaf. The problem is, the image itself is obtained from a url which takes in two parameters.

Sample:

<img src="/products/images?categoryId=1&image=1" />

The trouble is, the image parameter is generated dynamically and hence I need to use a thymeleaf expression there. Therefore I tried something like this:

<img th:src="@{'/products/images?categoryId=1&image=' + ${product.id}}" />

But when I run this, I get the following message:

Exception parsing document: template="product-list", line 104 - column 59

Which points to the location where the '&' symbol occurs. Now, I have tried using '& amp;' but then, the url becomes something like

/products/images?categoryId=1&amp;image=1

Obviously, this is not going to work.

So how else do I make a valid link with two parameters using thymeleaf then?

like image 295
shyam Avatar asked Apr 29 '15 08:04

shyam


2 Answers

This can easily done by Thymeleaf. Don't concatenate strings and simply use @{'/products/images'(categoryId=1, image= ${product.id})}

See the documentation.

like image 63
niels Avatar answered Sep 18 '22 04:09

niels


The way that you escape an ampersand & in any html attribute is &amp;. Actually you should always escape ampersands in all html attributes whether you are using Thymeleaf or not.

See this question for more details and references: Do I encode ampersands in <a href...>?

like image 33
John Krueger Avatar answered Sep 18 '22 04:09

John Krueger