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?

shyam
  • 1,348
  • 4
  • 19
  • 37

2 Answers2

12

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

See the documentation.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
niels
  • 7,321
  • 2
  • 38
  • 58
  • 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 Apr 29 '15 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...>?

Community
  • 1
  • 1
John Krueger
  • 1,436
  • 1
  • 10
  • 4