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

时间:2018-02-25 13:38:47

标签: java spring weblogic freemarker

我使用Apache freemarker配置电子邮件模板。请在下面找到此模板的内容:

<!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类,其中包含一个如下所示的实例字段,并带有正确的getter / setter:

private Double itemPrice;

我的电子邮件已经发送但没有任何正文内容。在我的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声明引起的。 我无法弄清楚确切的语法错误。任何人都有任何合适的解决方案吗?

1 个答案:

答案 0 :(得分:1)

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

更改你的if语句:

<#if item.itemPrice gt 40>

检查freemarker&#39; comparison

  

对于数字和日期,时间和日期时间值,您还可以使用&lt ;,&lt; =,&gt; =和&gt;。你不能将它们用于字符串!例如:

     

&lt; #if x&lt; = 12&gt; x小于或等于12

     

&gt; =和&gt;存在问题。 FreeMarker解释&gt;字符   作为FTL标签的结束字符..   ,例如&lt; #if x gt y&gt;。将表达式放入括号中的另一个技巧就像在&lt; #if(x&gt; y)&gt;中一样,虽然它被认为不那么优雅。

相关问题