Thymeleaf和内联脚本SAXParseException [英] Thymeleaf and inline scripts SAXParseException
问题描述
我得到了一个使用百里香模板的页面,并且在使用内联脚本时页面加载时出现以下错误:
I got a page which uses thymeleaf template, and I'm getting the following error on page load when using inline scripts:
org.xml.sax.SAXParseException; lineNumber:270; columnNumber:85;元素的
内容必须包含格式正确的字符数据或
标记。
org.xml.sax.SAXParseException; lineNumber: 270; columnNumber: 85; The content of elements must consist of well-formed character data or markup.
第270行的代码
<script type="text/javascript" >
window.jQuery || document.write("<script src='assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
</script>
我尝试用<$ c替换document.write中的<,>符号$ c>& lt; & gt;
,该异常不再发生,但脚本不再加载
I have tried replacing "<", ">" symbols from document.write with <
>
, the exception doesn't occur anymore but the script is not loaded anymore
推荐答案
您需要为脚本添加CDATA标记,如下所示:
You need to add CDATA tags for the script like this:
<script type="text/javascript">
//<![CDATA[
window.jQuery || document.write("<script src='assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
//]]>
</script>
这些标签告诉thymeleaf的xml解析器,不应将两者之间的代码解释为XML标记。
The tags are telling thymeleaf's xml parser that the code between should not be interpreted as XML markup.
自Thymeleaf 3.0以来不再需要
这篇关于Thymeleaf和内联脚本SAXParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!