Search code examples
javajett

How to use JETT if tag to compare strings


I am using JETT's if tag to format an Excel file.

I want to use the below condition to compare a string.

<jt:if test="${thisVar == "this is an apple" }">I have an apple.</jt:if>

However, my Excel sheet keeps running into a TagParseException where "this is an apple" is an unrecognised attribute.

Is it possible to compare strings in a JETT if tag?


Solution

  • You need to either escape the double quotes or use single quotes to define your String constant.

    These examples work: single quotes for the String

    <jt:if test="${thisVar == 'this is an apple'}">I have an apple.</jt:if>
    

    Or escape the double quotes:

    <jt:if test="${thisVar == \"this is an apple\"}">I have an apple.</jt:if>
    

    Reference:

    JETT supports escaping literal double-quote characters with a backslash, and escaping backslash characters as well:

    \" => "
    \\ => \
    

    And for full reference the stacktrace you get on this error:

    Exception in thread "main" net.sf.jett.exception.TagParseException: Unrecognized attribute "x" for tag "if" at Sheet!Cell.
        at net.sf.jett.tag.BaseTag.checkAttributes(BaseTag.java:214)
        at net.sf.jett.tag.BaseTag.processTag(BaseTag.java:239)
        at net.sf.jett.transform.CellTransformer.transformCellTag(CellTransformer.java:302)
        at net.sf.jett.transform.CellTransformer.transform(CellTransformer.java:123)
        at net.sf.jett.transform.BlockTransformer.transform(BlockTransformer.java:73)
        at net.sf.jett.transform.BlockTransformer.transform(BlockTransformer.java:36)
        at net.sf.jett.transform.SheetTransformer.transform(SheetTransformer.java:150)
        at net.sf.jett.transform.SheetTransformer.transform(SheetTransformer.java:76)
        at net.sf.jett.transform.ExcelTransformer.transform(ExcelTransformer.java:405)
        at net.sf.jett.transform.ExcelTransformer.transform(ExcelTransformer.java:379)