0

I'm passing a bean QuoteOption into the model that has a method called getCachedImageUrl(url, width, height) that returns a URL for the image.

My Thymeleaf template:

<img th:src="${quoteOption.getCachedImageUrl(baseUrl,300,200)}" />

Generates

<img src="http://nitro:8080/image/nitro-resources-development/28f08e67-96c9-4bb4-8012-9e34040cc976.jpeg?width=300&amp;height=200" />

Notice the &amp; in the url parameters! Aaaargh! How do I instruct Thymeleaf to not escape the ampersand?

sparkyspider
  • 13,195
  • 10
  • 89
  • 133
  • 2
    That's correct HTML - the & _should_ be escaped as & – daiscog Nov 19 '15 at 10:21
  • See here: http://stackoverflow.com/questions/3705591/do-i-encode-ampersands-in-a-href – daiscog Nov 19 '15 at 10:23
  • 1
    @daiscog, I've been developing web applications for 20 years, and I never knew this. Used thymeleaf to send an email with `&` in the `href` attribute, and it works 100%. Thank you for educating me! – sparkyspider Nov 20 '15 at 13:02

1 Answers1

0

Bean with name someBean:

public class SomeBean {

       public String testMethod() {
            return "http://nitro:8080/image/nitro-resources-development/28f08e67-96c9-4bb4-8012-9e34040cc976.jpeg?width=300&height=200";
        }
}

In Thymeleaf :

  <img th:src="${@applicationCache.testMethod()}" id="_Bhuwan"/>
  <button onclick="alert($('#_Bhuwan').attr('src'))">See</button>

Two Case :

  1. In alert Show message as :

    http://nitro:8080/image/nitro-resources-development/28f08e67-96c9-4bb4-8012-9e34040cc976.jpeg?width=300&height=200

  2. In View Page Source or Edit HTML in Browser. Generate :

    <img src="http://nitro:8080/image/nitro-resources-development/28f08e67-96c9-4bb4-8012-9e34040cc976.jpeg?width=300&amp;height=200">

So that actual value in src attribute of img tag is http://nitro:8080/image/nitro-resources-development/28f08e67-96c9-4bb4-8012-9e34040cc976.jpeg?width=300&height=200 in this case. But when we try see value using View Page source or edit html through browser then Browser may escape & char to &amp;.

Tested On Themeleaf version is 2.1.4.RELEASE in Spring 4.

Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33