[转]Freemarker数据类型转换
2016-03-29 09:22
531 查看
转至:http://blog.sina.com.cn/s/blog_667ac0360102eaz8.html
// 测试程序
package myTest;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FtlTest {
public static void main(String[] args) throws Exception {
Map<String, Object> dataMap = getModel_1();
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(FtlTest.class, "/ftl/");
Template template = configuration.getTemplate("test.ftl");
template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
dataMap = getModel_2();
template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
dataMap = getModel_3();
template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
}
public static Map<String, Object> getModel_1() {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("data_string", "string");
dataMap.put("data_int", 3);
dataMap.put("data_float", 3.333);
dataMap.put("data_boolean", true);
dataMap.put("data_date", new Date());
return dataMap;
}
public static Map<String, Object> getModel_2() {
Map<String, Object> dataMap = new HashMap<String, Object>();
return dataMap;
}
public static Map<String, Object> getModel_3() {
Map<String, Object> dataMap = new HashMap<String, Object>();
SimpleDateFormat df = new SimpleDateFormat("yyyy");
Date year = null;
try {
year = df.parse("2013");
} catch (ParseException e) {
throw new RuntimeException(e);
}
dataMap.put("data_date", year);
return dataMap;
}
}
// 模板
<#escape x as x?default("")>
数据类型测试
data_string=${data_string}
data_int=${data_int}
data_float=${data_float}
data_boolean=<#if data_boolean?exists>${data_boolean?string}</#if>
data_date=<#if data_date?exists>${data_date?string('yyyy-MM-dd')}</#if>
</#escape>
// 输出
数据类型测试
data_string=string
data_int=3
data_float=3.333
data_boolean=true
data_date=2013-09-25
数据类型测试
data_string=
data_int=
data_float=
data_boolean=
data_date=
数据类型测试
data_string=
data_int=
data_float=
data_boolean=
data_date=2013-01-01
说明:freemarker不会处理null数据,null会报错;使用<#escape标签,来为所有的插值做转换;转换调用了default内置函数,将null填充为“”空字符串;这样,如果在插值处要调用内置函数,就应该先使用<#if标签先判断是否存在了;布尔型、日期型默认不会自动转换为字符串,需要内置函数处理,在调用内置函数前要做是否存在的验证;最后,以上测试,日期型类型转换是根据Date类型来转换的,string内置函数可以比较灵活地使用自定义格式显示;
// 测试程序
package myTest;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FtlTest {
public static void main(String[] args) throws Exception {
Map<String, Object> dataMap = getModel_1();
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(FtlTest.class, "/ftl/");
Template template = configuration.getTemplate("test.ftl");
template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
dataMap = getModel_2();
template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
dataMap = getModel_3();
template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
}
public static Map<String, Object> getModel_1() {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("data_string", "string");
dataMap.put("data_int", 3);
dataMap.put("data_float", 3.333);
dataMap.put("data_boolean", true);
dataMap.put("data_date", new Date());
return dataMap;
}
public static Map<String, Object> getModel_2() {
Map<String, Object> dataMap = new HashMap<String, Object>();
return dataMap;
}
public static Map<String, Object> getModel_3() {
Map<String, Object> dataMap = new HashMap<String, Object>();
SimpleDateFormat df = new SimpleDateFormat("yyyy");
Date year = null;
try {
year = df.parse("2013");
} catch (ParseException e) {
throw new RuntimeException(e);
}
dataMap.put("data_date", year);
return dataMap;
}
}
// 模板
<#escape x as x?default("")>
数据类型测试
data_string=${data_string}
data_int=${data_int}
data_float=${data_float}
data_boolean=<#if data_boolean?exists>${data_boolean?string}</#if>
data_date=<#if data_date?exists>${data_date?string('yyyy-MM-dd')}</#if>
</#escape>
// 输出
数据类型测试
data_string=string
data_int=3
data_float=3.333
data_boolean=true
data_date=2013-09-25
数据类型测试
data_string=
data_int=
data_float=
data_boolean=
data_date=
数据类型测试
data_string=
data_int=
data_float=
data_boolean=
data_date=2013-01-01
说明:freemarker不会处理null数据,null会报错;使用<#escape标签,来为所有的插值做转换;转换调用了default内置函数,将null填充为“”空字符串;这样,如果在插值处要调用内置函数,就应该先使用<#if标签先判断是否存在了;布尔型、日期型默认不会自动转换为字符串,需要内置函数处理,在调用内置函数前要做是否存在的验证;最后,以上测试,日期型类型转换是根据Date类型来转换的,string内置函数可以比较灵活地使用自定义格式显示;
相关文章推荐
- c的详细学习(1)C语言概述
- YII动态模型(动态表名)支持分析
- 【图像处理】H.264开源解码器评测
- vs2010错误:验证时出错。HRESULT = '8000000A' 的解决办法
- 代码大全_第一部分: 打好基础
- bzoj4407于神之怒
- Zookeeper昨天还可以启动今天提示: Cannot open channel to 1 at election address /192.168.253.130:3888 java.net.Co
- How to enable time stamps for logs in 'dmesg' on Gaia and SecurePlatform
- 用extjs写一个登录界面
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 设计模式 ( 十二 ) 职责链模式(Chain of Responsibility)(对象行为)
- Oracle trunc()函数的用法
- 纪念一下,在新浪云的日子
- Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]
- 密码技术
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
- 剑指offer 10 矩形覆盖
- Asp.Net Web API 2第十八课——Working with Entity Relations in OData
- 为其他对象提供一种代理以控制对这个对象的访问-代理模式
- CentOS 6.7配置静态ip和组建虚拟局域网