freemarker(ftl)模板開發常用方法(迭代,數值格式化等)


1.if-else常用

<#if signStatus??> //如果signStatus值存在
   <#if signStatus=='2'>是<#else>否</#if> <#else> 
  否
</#if>

 if-elseif-else

 1 <#if condition>
 2   ...
 3 <#elseif condition2>
 4   ...
 5 <#elseif condition3>
 6   ...
 7 ...
 8 <#else>
 9   ...
10 </#if>

 

2.時間格式化

時間戳轉日期 

  ${time?number_to_datetime}

  ${time?number*1000} 時間戳乘1000

日期格式化

  ${dateTime?string('yyyy-MM-dd hh:mm:ss')}   格式化為 2020-11-12 15:05:29

  ${dateTime?string('yyyy-MM-dd ')}   格式化為 2020-11-12

3.數值格式化

數值如果有小數保留兩位小數,否則補0兩位,例如128.81, 688則為 688.00

${((totalInvoice.amount)!0)?string('###,##0.00')}
特別需要注意的是 ?前面的數值需要括號()
比如:${additionsTotalAmt!0?string('###,##0.00')}無效果,而正確的是 ${(additionsTotalAmt!0)?string('###,##0.00')}
另外,
 1 ${num?string('0.00')}
 2 如果小數點后不足兩位,用 0 代替
 3 
 4 ${num?string('#.##')}
 5 如果小數點后多余兩位,就只保留兩位,否則輸出實際值
 6 輸出為:1239765.46
 7 
 8 ${num?string(',###.00')}
 9 輸出為:1,239,765.46
10 整數部分每三位用 , 分割,並且保證小數點后保留兩位,不足用 0 代替 11 
12 ${num?string(',###.##')}
13 輸出為:1,239,765.46
14 整數部分每三位用 , 分割,並且小數點后多余兩位就只保留兩位,不足兩位就取實際位數,可以不不包含小數點
15 
16 ${num?string('000.00')}
17 輸出為:012.70
18 整數部分如果不足三位(000),前面用0補齊,否則取實際的整數位
19 
20 ${num?string('###.00')}
21 等價於
22 ${num?string('#.00')}
23 輸出為:12.70   
參考https://blog.csdn.net/qq_32534855/article/details/67631788
 1 ${price?string.currency}
 2 
 3 對price進行格式化,顯示為貨幣格式 4 
 5 比如:price=1
 6 
 7 輸出:¥1.00
 8 
 9 ${price?string.percent}
10 
11 對price進行格式化,顯示為百分比12 
13 比如:price=1
14 
15 輸出:100%

 

4.list迭代

 <#list 0..(bigTypeList!?size-1) as i>
    <#if bigTypeList[i].code==settleCheckBillVO.bigType>
         ${bigTypeList[i].name}
    </#if>

</#list>
list迭代方式一 <#list users as user>
  <p>${user}
</#list> list迭代方式二 <#list 1..10 as x>
  ${x}
  <#if x == 3>
    break的用法     <#break>   </#if> </#list>
list迭代方式三
<#list users> <ul> <#items as user> <li>${user}</li> </#items> </ul> <#else> <p>No users </#list>

5.List<Map<String, List<Map<String, Object>>>> 的迭代及map的使用

${map.key1} 獲取key1相應value值,在<#if> <#list> 等之中,則直接使用 map.key1

<#list mergeList as map>   <#if map.addList?size != 0>   <#list map.addList as itemMap>     <div style="height: 24px;">-${itemMap.name!}</div>     <div style="height: 24px;">${((map.addList[i].amount)!0)?string('###,##0.00')}</div>   </#list>   </#if> </#list>

封裝mergeList的方法
private List<Map<String, List<Map<String, Object>>>> mergeList(List<List<Map<String, Object>>> add, List<List<Map<String, Object>>> sub) {

List<Map<String, List<Map<String, Object>>>> newList = Lists.newArrayList();
if (CollectionUtils.isEmpty(add) && CollectionUtils.isEmpty(sub)) {
return newList;
}
//查找最大size
int length = add.size() > sub.size() ? add.size() : sub.size();
for (int i = 0; i < length; i++) {
//若有項則進行添加,否則置為空數組
List<Map<String, Object>> addList = add.size() > i ? add.get(i) : Lists.newArrayList();
List<Map<String, Object>> subList = sub.size() > i ? sub.get(i) : Lists.newArrayList();
//保持有序的map,先加項后減項
Map<String, List<Map<String, Object>>> map = Maps.newLinkedHashMap();
map.put("addList", addList);
map.put("subList", subList);
newList.add(map);
}
return newList;
}

 

6.數值為空導致的報錯的處理方式

添加以下紅色的部分,當為對象時需要加括號(一些可能為空的數據需要這么處理)
${saleUnifiedIdentityCode!""}
${((totalInvoice.amount)!0)?string('###,##0.00')}

 

 1 <#if receiptVOList?size != 0>
 2         <div style="font-size: 16px;
 3     font-weight: bold;padding:10px 0">匹配通過收據明細 <span style="margin-left: 20px">共${(receiptVOList)?size!0}張</span></div>
 4         <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tb">
 5             <tr>
 6                 <td width="16%" class="tl" style="border-width: 1px 1px 1px 1px">發票類型</td>
 7                 <td width="10%" class="tc" style="border-width: 1px 1px 1px 0">開票日期<i></i></td>
 8                 <td width="12%" class="tc" style="border-width: 1px 1px 1px 0">收據編號<i></i></td>
 9                 <td width="12%" class="tc" style="border-width: 1px 1px 1px 0">含稅金額<i></i></td>
10                 <td width="26%" class="tc" style="border-width: 1px 1px 1px 0">備注<i></i></td>
11             </tr>
12 
13             <#if receiptVOList?size != 0>
14                 <#list 0..(receiptVOList!?size-1) as i>
15                     <tr class="<#if i%2==0>space</#if>"
16                         style=" <#if (receiptVOList[i].totalAmount<0)>color: red</#if>">
17                         <td class="tl" style="border-width: 0px 1px 1px 1px"><p>收據</p></td>
18                         <td class="tc" style="border-width: 0px 1px 1px 0px">
19                             <p>${(receiptVOList[i].openInvoiceTime?number_to_datetime)?string('yyyy-MM-dd')!}</p></td>
20                         <td class="tc" style="border-width: 0px 1px 1px 0px"><p>${receiptVOList[i].billNo!}</p></td>
21                         <td class="tc" style="border-width: 0px 1px 1px 0px">
22                             <p>${((receiptVOList[i].totalAmount)!0)?string('###,##0.00')}</p></td>
23                         <td class="tc" style="border-width: 0px 1px 1px 0px"><p>${(receiptVOList[i].remark)!}</p></td>
24                     </tr>
25                 </#list>
26             </#if>
27         </table>
28     </#if>

 

7.字符串操作函數(參考下面,即可大概清楚ftl中函數如何使用)

${“strabg”?replace(“ab”,”in”)} 結果為string  
${“string”?contains(“ing”)?string} 結果為true  
注意:布爾值必須轉換為字符串才能輸出  
${“string”?index_of(“in”) 結果為3 
${“string”?index_of(“ab”) 結果為-1 
length返回字符串的長度 ${“string”?length}結果為6 
lower_case將字符串轉為小寫  
${“STRING”?lower_case}à結果為string  
upper_case將字符串轉為大寫  
${“string”?upper_case}à結果為STRING  
ends_with 判斷某個字符串是否由某個子串結尾,返回布爾值。  
${“string”?ends_with(“ing”)?string} 返回結果為true  

 

--freemarker在線手冊地址參考 http://freemarker.foofun.cn/ref_directive_list.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM