Thimeleaf display error when using ampersand in script tag

I want to add google javascript to Thymeleaf template, for example:

https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places 

It throws an exception:

 org.xml.sax.SAXParseException; lineNumber: 209; columnNumber: 93; The reference to entity "key" must end with the ';' delimiter 

I tried changing & to & but nothing has changed.

Your help will be greatly appreciated. Thanks!

+5
source share
3 answers

Thymeleaf uses an XML parser, and the & character is considered a special character in XML. You must replace & with your uniform XML & . Your URL will be as follows:

 https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places 

In thimeleaf 3, this will not be a problem, since they wrote a new parser for the thimeleaf.

+2
source

It was a mistake in Timeleaf, but it has been fixed since 2.1.4.

Look at the issue . (The problem described in this question ).

0
source

You have several options:

1) Do not use th:src at all.

2) Since the URL is absolute, you can add it on the server side using the static method:

 <script th:src="${@urlService.getMapsUrl()}">...</script> 

3) Use rewrite filter .

Not sure if the issue is fixed in Thymeleaf 3, but it might be worth a quick update and a look.

0
source

Source: https://habr.com/ru/post/1243282/


All Articles