使用 freemarker 基于模型数据的行突出显示

Row highlighting based on Model Data using freemarker

我正在使用 Apache freemarker 配置电子邮件 template.Please 在下面找到此模板的内容:

<!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>

我有一个 CartDTO class,其中包含一个实例字段,如下所示,具有适当的 getter/setter:

private Double itemPrice;

我的电子邮件正在发送,但没有任何正文content.In我的 weblogic 服务器日志我收到以下异常:

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.)

我认为问题是由于 #if 语句引起的。 我无法弄清楚确切的语法 error.can 有人对此有任何合适的解决方案吗?

您最好使用 gt 进行数值比较,不要使用引号(这将标识为字符串)。

更改您的 if 语句:

<#if item.itemPrice gt 40>

检查 freemarker 的 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.