Greater than > Less than < inside thymeleaf javascript... Error: The content of elements must consist of well-formed character data or markup

Greater than > Less than < inside thymeleaf javascript... Error: The content of elements must consist of well-formed character data or markup

当我尝试在 thymeleaf javascript.

中插入 <> 运算符时出现此错误

我的代码

<script th:inline="javascript">
    $(document).ready(function () {
        ...
        if(timeRemain < 0){
            ...
        }
        ...
        var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
        ...         
    });
</script>

错误信息

org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.

我该如何解决这个问题?

此问题已通过将 CDATA 添加到 script 标签解决,如下所示

<script th:inline="javascript">
    /*<![CDATA[*/
    ...  
    ...
    ...
    /*]]>*/
</script>

除了使用 CDATA,您还可以编码 < as &lt;> as &gt;