Search code examples
javaspringweblogicfreemarker

Row highlighting based on Model Data using freemarker


I am using Apache freemarker for configuring an email template.Please find below the content of this template:

<!DOCTYPE html>
<html>
<body>
<h4>Dear User,</h4>
<br/>
<p>You have added following items into cart</p>

<table>
   <tr>
    <th>Item Name</th>  
    <th>Item Quantity</th>
    <th>Item Price</th>
  </tr>
   <#list itemList as item>
      <tr>
        <td>${item.itemName}</td> 
        <td>${item.itemQuantity}</td>
         <#if "item.itemPrice > 40">
          <td bgcolor="green">${item.itemPrice}</td>
         <#else>
          <td bgcolor="blue">${item.itemPrice}</td>
         <#if>
      </tr>
    </#list>
   </table>
  </body>
 </html>

I have a CartDTO class which contains an instance field as below with proper getter/setter:

private Double itemPrice;

My email is getting delivered but without any body content.In my weblogic server log I am getting following exception:

Exception occured while processing fmtemplate:Syntax error in template "cartInfo.ftl" in line 22, column 26:
 #if is an existing directive, but the tag is malformed.  (See FreeMarker Manual / Directive Reference.)

I think the problem arises due to #if statement. I am unable figure out the exact syntactical error.can anyone has any suitable solution to this?


Solution

  • You better use gt for numerical comparison and don't use quotes (which will identify as string).

    change your if statement:

    <#if item.itemPrice gt 40>
    

    Check freemarker's comparison:

    For numerical and date, time and date-time values you can also use <, <=, >= and >. You can't use them for strings! Example:

    <#if x <= 12> x is less or equivalent with 12

    There's a problem with >= and >. FreeMarker interprets the > character as the closing character of the FTL tag.. , like in <#if x gt y>. Another trick it to put the expression into parentheses like in <#if (x > y)>, although it's considered to be less elegant.