Thymeleaf: How to use boolean operator in JavaScript using Thymeleaf

泪湿孤枕 提交于 2020-01-01 05:39:07

问题


I am using thymeleaf, in javascript using th:inline="javascript", but when we add Boolean conditions in java script thymeleaf thow an exception as below:

org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 22; The entity name must immediately follow the '&' in the entity reference.
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(XMLDocumentFragmentScannerImpl.java:1845)
.............................

Below is my javascript code:

<script type="text/javascript" th:inline="javascript">
    $(document).ready(function(){
        $('.fancybox').fancybox({
            'width' :'623px',
            'maxHeight':'156px'
        });

        var catId = $("#category").val();
        if(catId != null && catId != ''){
            findSubCategories();
            /*<![CDATA[*/
                var subCatId = /*[[${searchProductDto.subCategory}]]*/
            /*]]>*/
            debugger;
            if(subCatId != null){
                $("#subCategory").val(subCatId);
            }
        }
    });
...............................

how we use & operator in thymeleaf?


回答1:


wrap the if block with <![CDATA[ block

<script type="text/javascript" th:inline="javascript">
var a =b = true;
/*<![CDATA[*/
if(a && b){
    alert('Yea');
}/*]]>*/
</script>



回答2:


Use the <!CDATA[ protection around the entire contents of the script tag.



来源:https://stackoverflow.com/questions/28718421/thymeleaf-how-to-use-boolean-operator-in-javascript-using-thymeleaf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!