Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: How to use boolean operator in JavaScript using Thymeleaf

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?

like image 891
Harmeet Singh Taara Avatar asked Feb 25 '15 11:02

Harmeet Singh Taara


People also ask

How do you use Boolean in Thymeleaf?

#bools. xxxAnd(collection) returns true if all elements in the argument is evaluated to true. #bools. xxxOr(collection) returns true if any element in the argument is evaluated to true.

How do you check Boolean value in if condition in Thymeleaf?

You can access model attributes by using variable expression ( ${modelattribute. property} ). And, you can use th:if for conditional checking. Hope this helps.

Can you use Thymeleaf in JavaScript?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.

How do you use condition in Thymeleaf?

Simple Conditionals: "if" and "unless" Thymeleaf provides th:if and th:unless conditional attributes to exclude elements based on a provided condition in the rendered document. Thymeleaf engine will evaluate the condition th:if="${user. active}" defined on the tr HTML element.


1 Answers

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>
like image 132
Jayaram Avatar answered Oct 02 '22 12:10

Jayaram