在脚本标记中使用&符号时出现Thymeleaf渲染错误

时间:2016-02-18 03:27:07

标签: java spring-mvc web thymeleaf

我想将谷歌地图javascript添加到Thymeleaf模板中,如下所示:

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

抛出异常:

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

我尝试将&更改为&,但没有任何改变。

非常感谢您的帮助。谢谢!

3 个答案:

答案 0 :(得分:4)

Thymeleaf使用XML解析器,字符&被视为XML中的特殊字符。您必须将&替换为其XML等效&。您的网址将为:

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

在thymeleaf 3中,因为他们为百里香叶写了一个新的解析器,所以它不会成为一个问题。

答案 1 :(得分:0)

这是Thymeleaf中的一个错误,但它自2.1.4以来就已修复。

看看这个issue。 (this question)中描述的问题。

答案 2 :(得分:0)

你有一些选择:

1)根本不要使用th:src

2)由于URL是绝对的,您可以使用静态方法在服务器端添加它:

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

3)使用rewrite filter

不确定问题是否在Thymeleaf 3中得到纠正,但值得快速升级并查看。