一 數據類型簡介
freemarker 模板中的數據類型由如下幾種:
1. 布爾型:等價於java中的boolean類型, 不同的是不能直接輸出,可以轉換成字符串再輸出
2. 日期型:等價於java中的Date類型, 不同之處在於不能直接輸出,需要轉換成字符串再輸出
3. 數值型:等價於java 中的int, float, double 等數值類型,有三種顯示形式:數值型(默認) 、貨幣型、百分比型
4. 字符串型:等價於java 中的字符串,有很多內置函數
5. sequence 類型:等價於java中的數組,list,set 等集合類型
6. hash 類型:等價於java 中的Map 類型
二、 數據類型示例
【1. 布爾型】:
1. 不能直接輸出布爾型的值, 必須轉換為string:${b?string}
2. 在if標簽中可以直接使用
<#if b>
b 的值為 true
</#if>
【2. 日期型】
1. 輸出日期:${currentDate?date}
2. 只輸出時間:${currentDate?time}
3. 輸出日期時間:${currentDate?datetime}
4. 格式化日期: ${currentDate?string('yyyy-MM-dd HH:mm:ss:S')}
【3. 數值型】:
2.1 Freemarker 中預定義了三種數字格式,貨幣,百分比,數字,默認為數字格式
貨幣::${0.3?string.currency}
百分比:${0.3?string.percent}
數字(默認):${0.3?string.number}
2.2 取整
1. 向上取整
3.4 --> ${3.4?ceiling}
3.5 --> ${3.5?ceiling}
2. 向下取整
3.4 --> ${3.4?floor}
3.5 --> ${3.5?floor}
3. 四舍五入
3.4 --> ${3.4?round}
3.5 --> ${3.5?round}
2.3 數字格式化, 使用0 表示不夠 由0 補齊, 用# 表示不夠不補齊
1. 保留兩位小數: 必須兩位,不夠補0, 當前一位為偶數時,五舍六入, 當前一位為基數時,四舍五入
0.135 -- > ${0.135?string('.00')}
0.125 -- > ${0.125?string('.00')}
0.1 -- > ${0.1?string('.00')}
2. 保留兩位小數: 最多兩位,不夠不補0, 當前一位為偶數時,五舍六入, 當前一位為基數時,四舍五入
0.135 -- > ${0.135?string('#.##')}
0.125 -- > ${0.125?string('#.##')}
0.1 -- > ${0.1?string('#.##')}
3. 格式化整數, 用0 表示必須三位整數,不夠由0 補齊
12.1 -- > ${12.1?string('000.00')}
12.125 -- > ${12.125?string('000.00')}
12.135 -- > ${12.135?string('000.00')}
4. 格式化整數, 用0 表示必須三位整數,不夠由0 補齊, 一個# 和 多個# 是一樣的
12.1 -- > ${12.1?string('#.00')}
12.125 -- > ${12.125?string('#.00')}
12.135 -- > ${12.135?string('#.00')}
5. 千位分割
123456789 --> ${123456789?string(',###')}
123456789 --> ${123456789?string(',####')}
2.4 數字轉換成字符串:
數字轉換成字符串后,就可以直接用字符串的內置函數了
1234 -- > ${123?string}
1234 -- > ${123?string[0]}
<#-- ${123[2]} 報錯 -->
【4. 字符串型】
4.1 截取字符串subString(start,end):"hello,wold"
1. 截取6~end: ${"hello,wold"?substring(6)}
2. 截取0~5: ${"Hello,World"?substring(0,5)}
4.2 字母大小寫轉換
1. 首個單詞的首字母大寫: ${"hello world"?cap_first}
2. 首個單詞的首個字母母小寫: ${"Hello World"?uncap_first}
3. 所有單詞首字母大寫:${"hello world"?capitalize}
4. 字符串大寫: ${"hello,world"?upper_case}
5. 字符串小寫:${"hello,world"?lower_case}
4.3 判斷是否以xxx 結尾
1. ${"hello,world"?ends_with("world")?string}
2. <#if "hello,world"?ends_with("world")>
hello,world 以字符串 world 結尾
</#if>
4.4 判斷是否以xxx 開頭
1. ${"hello,world"?starts_with("hello")?string}
2. <#if "hello,world"?starts_with("hello")>
hello,world 以字符串 hello 開頭
</#if>
4.5 返回字符串長度
${"hello,world"?length}
4.6 是否包含子串
1. 返回為布爾值,布爾值不能直接輸出,必須轉換為string
${"hello,world"?contains("llo")?string};
2. <#if "hello,world"?contains("llo")>
"hello,world" 包含子串 "llo"
</#if>
4.7 去除首尾空格
字符串:${" hello,world "?trim}
4.8 替換字符串
${"hello,world"?replace("o","0")}
4.9 查詢字符串第一次出現的索引位置,如果不存在返回0
${"hello,world"?index_of("o")}
${"hello,world"?index_of("aaa")}
4.10 字符串分割數組
<#assign citys="beijing,tianjin,shanghai"?split(",")/>
<#list citys as city>
${city_index} --> ${city}
</#list>
4.11 輸出單個字母
${"hello"[0]}
【5. sequence】
1. 獲取第一個元素:sequence?first
array: ${cityArray?first}
list: ${cityList?first}
set: ${citySet?first}
2. 獲取最后一個元素:sequence?last
array: ${cityArray?last}
list: ${cityList?last}
set: ${citySet?last}
3. 返回sequence 的大小sequence?size
array: ${cityArray?size}
list: ${cityList?size}
set: ${citySet?size}
4. 排序:sequence?sort
4.1 sequence 元素為基本元素時(能轉換為String的元素,非sequence 和 hash 元素)
array:sort,reverse
正 序:<#list cityArray as city>${city},</#list>
倒 序:<#list cityArray?reverse as city>${city},</#list>
升 序:<#list cityArray?sort as city>${city},</#list>
降 序:<#list cityArray?sort?reverse as city>${city},</#list>
list:sort,reverse
正 序:<#list cityList as city>${city},</#list>
倒 序:<#list cityList?reverse as city>${city},</#list>
升 序:<#list cityList?sort as city>${city},</#list>
降 序:<#list cityList?sort?reverse as city>${city},</#list>
set:sort,reverse
正 序:<#list citySet as city>${city},</#list>
倒 序:<#list citySet?reverse as city>${city},</#list>
升 序:<#list citySet?sort as city>${city},</#list>
降 序:<#list citySet?sort?reverse as city>${city},</#list>
4.2 sequence 元素為JavaBean時
正 序:
<#list department.employees as employee>
${employee_index} --> ${employee.name} --> ${employee.age} --> ${employee.sex}
</#list>
逆 序:
<#list department.employees?reverse as employee>
${employee_index} --> ${employee.name} --> ${employee.age} --> ${employee.sex}
</#list>
按name屬性升序:
<#list department.employees?sort_by("name") as employee>
${employee_index} --> ${employee.name} --> ${employee.age} --> ${employee.sex}
</#list>
按name屬性降序:
<#list department.employees?sort_by("name")?reverse as employee>
${employee_index} --> ${employee.name} --> ${employee.age} --> ${employee.sex}
</#list>
5. 遍歷sequence, 包含索引值
array: <#list cityArray as city>
${city_index} --> ${city}
</#list>
list: <#list cityList as city>
${city_index} --> ${city}
</#list>
set: <#list citySet as city>
${city_index} --> ${city}
</#list>
6. 根據索引獲取sequence 元素
array: ${cityArray[0]}
list: ${cityList[0]}
set: ${citySet[0]}
【6. map 類型】
1. map長度:${cityMap?size};
2. map的keys:cityMap.keys 返回的是一個sequence,類似於數組,所以不能直接輸出,需要遍歷
<#assign mapKeys=cityMap?keys/>
<#list mapKeys as mapKey>
${mapKey}
</#list>
3. map的values: cityMap.values 返回的是一個sequence,類似於數組,所以不能直接輸出,需要遍歷
<#assign mapValues=cityMap?values/>
<#list mapValues as mapValue>
${mapValue}
</#list>
4. 遍歷map 元素: map 通過key獲取value的方法用[]
<#list cityMap?keys as key>
${key_index} --> ${key} --> ${cityMap[key]}
</#list>
【7. JavaBean 類型】
1. 獲取屬性:${department.id} --> ${department.name}
2. 級聯屬性:${department.employees[0].name} --> ${department.employees[0].age} --> ${department.employees[0].sex}
3. 遍歷數組:
<#list department.employees as employee>
${employee_index} --> ${employee.name} --> ${employee.age} --> ${employee.sex}
</#list>
4. 排序
<#list department.employees?sort_by("name") as employee>
${employee_index} --> ${employee.name} --> ${employee.age} --> ${employee.sex}
</#list>