簡體   English   中英

使用freemarker基於模型數據的行高亮顯示

[英]Row highlighting based on Model Data using 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語句引起的。 我無法弄清楚確切的語法錯誤。有人對此有任何合適的解決方案嗎?

您最好使用gt進行數值比較,不要使用引號(它將標識為字符串)。

更改您的 if 語句:

<#if item.itemPrice gt 40>

檢查 freemarker 的比較

對於數字和日期、時間和日期時間值,您還可以使用 <、<=、>= 和 >。 您不能將它們用於字符串! 例子:

<#if x <= 12> x 小於或等於 12

>= 和 > 有問題。 FreeMarker 將 > 字符解釋為 FTL 標簽的結束字符.. ,就像在 <#if x gt y> 中一樣。 另一個技巧是將表達式放入括號中,如 <#if (x > y)> ,盡管它被認為不太優雅。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM