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

 

posted @ 2020-11-19 15:33  Jeessu  阅读(2042)  评论(0编辑  收藏  举报