8

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?

2 Answers 2

12

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

See the documentation.

1
  • Wow! Thanks! Guess I didn't know what to search for. I was just searching for ways to get the '&' symbol to show up!
    – shyam
    Commented Apr 29, 2015 at 10:38
4

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...>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.