(二)Freemarker 基本数据类型
来源:互联网 发布:mac如何转换输入法 编辑:程序博客网 时间:2024/05/22 00:44
一 数据类型简介
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}
3.4 --> ${3.4?floor}
3.5 --> ${3.5?floor}
3.4 --> ${3.4?round}
3.5 --> ${3.5?round}
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')}
123456789 --> ${123456789?string(',###')}
123456789 --> ${123456789?string(',####')}
数字转换成字符串后,就可以直接用字符串的内置函数了
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>
${"hello"[0]}
【5. sequence】
1. 获取第一个元素:sequence?firstarray: ${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>
正 序:
<#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>
array: ${cityArray[0]}
list: ${cityList[0]}
set: ${citySet[0]}
【6. map 类型】
1. map长度:${cityMap?size};
<#assign mapKeys=cityMap?keys/>
<#list mapKeys as mapKey>
${mapKey}
</#list>
<#assign mapValues=cityMap?values/>
<#list mapValues as mapValue>
${mapValue}
</#list>
<#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>
三、测试用例
【1. ftl 文件】 将第二部分copy 一下,就是模板文件
【2.测试用例】
package org.zgf.learn.freemarker;import java.io.File;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.ArrayList;import java.util.Arrays;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import org.junit.After;import org.junit.Test;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateExceptionHandler;/** * 测试数据类型 */public class Test_02_dataType {private static Configuration cfg;private static final String TEMPLATEFILENAME = "src/test/resources/templates";private static Template dateTmp;private static Map<String,Object> root = new HashMap<>();;static{try{//初始化参数cfg = new Configuration(Configuration.VERSION_2_3_22);cfg.setDirectoryForTemplateLoading(new File(TEMPLATEFILENAME));cfg.setDefaultEncoding("UTF-8");cfg.setTemplateUpdateDelayMilliseconds(0);cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);dateTmp = cfg.getTemplate("datatype.ftl");}catch(Exception ex){ex.printStackTrace();}}@Afterpublic void tearDown() throws Exception{//输出数据Writer out = new OutputStreamWriter(System.out); dateTmp.process(root, out);out.flush();out.close();}@Testpublic void test_1() throws Exception{//1. 布尔型root.put("b", true);//2. 数值型root.put("currentDate", new Date());//3. 数组类型String[] cityArray = new String[]{"beijing" , "tianjin" , "shanghai"};root.put("cityArray", cityArray);//4. list 类型List<String> cityList = Arrays.asList(cityArray);root.put("cityList",cityList);//5. set 类型Set<String> citySet = new HashSet<>(cityList);root.put("citySet", citySet);//6. map 类型Map<String, String> cityMap= new HashMap<>();cityMap.put("BJ", "beijing");cityMap.put("TJ", "tianjin");cityMap.put("SH", "shanghai");root.put("cityMap", cityMap);//7.JavaBean Department department = new Department();department.setId(101);department.setName("开发部");List<Employee> emps = new ArrayList<>();for(int i=0; i<10; i++){Employee emp = new Employee("zong_" + i, 20 + i, "man");emps.add(emp);}department.setEmployees(emps);root.put("department", department);}}
【3. 1JavaBean--Department.java】
public class Department {private Integer id;private String name;private List<Employee> employees; //setter&&getter...}
【3. 2JavaBean--Employee.java】
public class Employee {private String name;private int age;private String sex; <pre name="code" class="java"> //setter&&getter...}
四 测试结果
二、 数据类型示例【1. 布尔型】: 1. 不能直接输出布尔型的值, 必须转换为string:true 2. 在if标签中可以直接使用 b 的值为 true【2. 日期型】 1. 输出日期:2016-3-4 2. 只输出时间:17:11:40 3. 输出日期时间:2016-3-4 17:11:40 4. 格式化日期: 2016-03-04 17:11:40:580【3. 数值型】: 2.1 Freemarker 中预定义了三种数字格式,货币,百分比,数字,默认为数字格式 货币::¥0.30 百分比:30% 数字(默认):0.3 2.2 取整 1. 向上取整 3.4 --> 4 3.5 --> 4 2. 向下取整 3.4 --> 3 3.5 --> 3 3. 四舍五入 3.4 --> 3 3.5 --> 4 2.3 数字格式化, 使用0 表示不够 由0 补齐, 用# 表示不够不补齐 1. 保留两位小数: 必须两位,不够补0, 当前一位为偶数时,五舍六入, 当前一位为基数时,四舍五入 0.135 -- > .14 0.125 -- > .12 0.1 -- > .10 2. 保留两位小数: 最多两位,不够不补0, 当前一位为偶数时,五舍六入, 当前一位为基数时,四舍五入 0.135 -- > 0.14 0.125 -- > 0.12 0.1 -- > 0.1 3. 格式化整数, 用0 表示必须三位整数,不够由0 补齐 12.1 -- > 012.10 12.125 -- > 012.12 12.135 -- > 012.14 4. 格式化整数, 用0 表示必须三位整数,不够由0 补齐, 一个# 和 多个# 是一样的 12.1 -- > 12.10 12.125 -- > 12.12 12.135 -- > 12.14 5. 千位分割 123456789 --> 123,456,789 123456789 --> 1,2345,6789 2.4 数字转换成字符串: 数字转换成字符串后,就可以直接用字符串的内置函数了 1234 -- > 123 1234 -- > 1 【4. 字符串型】 4.1 截取字符串subString(start,end):"hello,wold" 1. 截取6~end: wold 2. 截取0~5: Hello 4.2 字母大小写转换 1. 首个单词的首字母大写: Hello world 2. 首个单词的首个字母母小写: hello World 3. 所有单词首字母大写:Hello World 4. 字符串大写: HELLO,WORLD 5. 字符串小写:hello,world 4.3 判断是否以xxx 结尾 1. true 2. hello,world 以字符串 world 结尾 4.4 判断是否以xxx 开头 1. true 2. hello,world 以字符串 hello 开头 4.5 返回字符串长度 11 4.6 是否包含子串 1. 返回为布尔值,布尔值不能直接输出,必须转换为string true; 2. "hello,world" 包含子串 "llo" 4.7 去除首尾空格 字符串:hello,world 4.8 替换字符串 hell0,w0rld 4.9 查询字符串第一次出现的索引位置,如果不存在返回0 4 -1 4.10 字符串分割数组 0 --> beijing 1 --> tianjin 2 --> shanghai 4.11 输出单个字母 h 【5. sequence】 1. 获取第一个元素:sequence?first array: beijing list: beijing set: tianjin 2. 获取最后一个元素:sequence?last array: shanghai list: shanghai set: shanghai 3. 返回sequence 的大小sequence?size array: 3 list: 3 set: 3 4. 排序:sequence?sort 4.1 sequence 元素为基本元素时(能转换为String的元素,非sequence 和 hash 元素) array:sort,reverse 正 序:beijing,tianjin,shanghai, 倒 序:shanghai,tianjin,beijing, 升 序:beijing,shanghai,tianjin, 降 序:tianjin,shanghai,beijing, list:sort,reverse 正 序:beijing,tianjin,shanghai, 倒 序:shanghai,tianjin,beijing, 升 序:beijing,shanghai,tianjin, 降 序:tianjin,shanghai,beijing, set:sort,reverse 正 序:tianjin,beijing,shanghai, 倒 序:shanghai,beijing,tianjin, 升 序:beijing,shanghai,tianjin, 降 序:tianjin,shanghai,beijing, 4.2 sequence 元素为JavaBean时 正 序: 0 --> zong_0 --> 20 --> man 1 --> zong_1 --> 21 --> man 2 --> zong_2 --> 22 --> man 3 --> zong_3 --> 23 --> man 4 --> zong_4 --> 24 --> man 5 --> zong_5 --> 25 --> man 6 --> zong_6 --> 26 --> man 7 --> zong_7 --> 27 --> man 8 --> zong_8 --> 28 --> man 9 --> zong_9 --> 29 --> man 逆 序: 0 --> zong_9 --> 29 --> man 1 --> zong_8 --> 28 --> man 2 --> zong_7 --> 27 --> man 3 --> zong_6 --> 26 --> man 4 --> zong_5 --> 25 --> man 5 --> zong_4 --> 24 --> man 6 --> zong_3 --> 23 --> man 7 --> zong_2 --> 22 --> man 8 --> zong_1 --> 21 --> man 9 --> zong_0 --> 20 --> man 按name属性升序: 0 --> zong_0 --> 20 --> man 1 --> zong_1 --> 21 --> man 2 --> zong_2 --> 22 --> man 3 --> zong_3 --> 23 --> man 4 --> zong_4 --> 24 --> man 5 --> zong_5 --> 25 --> man 6 --> zong_6 --> 26 --> man 7 --> zong_7 --> 27 --> man 8 --> zong_8 --> 28 --> man 9 --> zong_9 --> 29 --> man 按name属性降序: 0 --> zong_9 --> 29 --> man 1 --> zong_8 --> 28 --> man 2 --> zong_7 --> 27 --> man 3 --> zong_6 --> 26 --> man 4 --> zong_5 --> 25 --> man 5 --> zong_4 --> 24 --> man 6 --> zong_3 --> 23 --> man 7 --> zong_2 --> 22 --> man 8 --> zong_1 --> 21 --> man 9 --> zong_0 --> 20 --> man 5. 遍历sequence, 包含索引值 array: 0 --> beijing 1 --> tianjin 2 --> shanghai list: 0 --> beijing 1 --> tianjin 2 --> shanghai set: 0 --> tianjin 1 --> beijing 2 --> shanghai 6. 根据索引获取sequence 元素 array: beijing list: beijing set: tianjin【6. map 类型】 1. map长度:3; 2. map的keys:cityMap.keys 返回的是一个sequence,类似于数组,所以不能直接输出,需要遍历 SH TJ BJ 3. map的values: cityMap.values 返回的是一个sequence,类似于数组,所以不能直接输出,需要遍历 shanghai tianjin beijing 4. 遍历map 元素: map 通过key获取value的方法用[] 0 --> SH --> shanghai 1 --> TJ --> tianjin 2 --> BJ --> beijing 【7. JavaBean 类型】 1. 获取属性:101 --> 开发部 2. 级联属性:zong_0 --> 20 --> man 3. 遍历数组: 0 --> zong_0 --> 20 --> man 1 --> zong_1 --> 21 --> man 2 --> zong_2 --> 22 --> man 3 --> zong_3 --> 23 --> man 4 --> zong_4 --> 24 --> man 5 --> zong_5 --> 25 --> man 6 --> zong_6 --> 26 --> man 7 --> zong_7 --> 27 --> man 8 --> zong_8 --> 28 --> man 9 --> zong_9 --> 29 --> man 4. 排序 0 --> zong_0 --> 20 --> man 1 --> zong_1 --> 21 --> man 2 --> zong_2 --> 22 --> man 3 --> zong_3 --> 23 --> man 4 --> zong_4 --> 24 --> man 5 --> zong_5 --> 25 --> man 6 --> zong_6 --> 26 --> man 7 --> zong_7 --> 27 --> man 8 --> zong_8 --> 28 --> man 9 --> zong_9 --> 29 --> man
- (二)Freemarker 基本数据类型
- (二)Freemarker 基本数据类型
- FreeMarker基本数据类型
- freemarker基本数据类型
- 基本数据类型(二)
- (二)基本数据类型
- freemarker的基本语法(二)
- 数据结构(二) --- 基本数据类型
- Swifit基本数据类型(二)
- (二)PCL基本数据类型
- python基本数据类型(二)
- symbian基本数据类型转化汇集(二)
- C++学习(二) 基本数据类型
- (二)基本数据类型和计算
- Python3基础(二) 基本数据类型
- (二)八中基本数据类型
- Swift快速入门(二)基本数据类型
- Java菜鸟教程 基本数据类型(二)
- DP之收了前几天的flag系列【树状数组优化
- C# 观察者模式.
- InnoDB: Unable to find the AUTOINC column
- c++ vector用法
- 先序建二叉树,中序遍历
- (二)Freemarker 基本数据类型
- GMM高斯混合模型
- 2015年总结下
- 今夕,何夕
- 设计模式整体总结
- bzoj 1012 [JSOI2008]最大数maxnumber
- 《Maven实战》学习总结(一)——Maven内行中的专家
- 登录界面获取用户名密码,在第二个页面读出该用户名,然后显示。
- POJ 2386 Lake Counting - DFS