freemaker简单演示
2015-12-13 22:02
316 查看
package com.whb.test;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* @author WHB
*
*/
public class TestFreeMarker {
@Test
public void testHello(){
//创建Configruation
Configuration cfg = new Configuration();
//System.out.println();
//new File("dd").mkdir();
//System.out.println(new File("ftl").getAbsolutePath());
//设置config中加载的模板路径
try {
cfg.setClassForTemplateLoading(TestFreeMarker.class, "/hello/ftl");
//获取模板文件,用于已经设置默认路径是/ftl
//此时hello.ftl就是ftl下的模板文件
Template temp = cfg.getTemplate("hello.ftl");
//创建数据文件,使用map来进行设置
Map<String,Object> root = new HashMap<String,Object>();
root.put("username", "校长");
//通过模板和文件输出相应的数据
temp.process(root, new PrintWriter(System.out));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* @author WHB
*
*/
public class TestFreeMarker {
@Test
public void testHello(){
//创建Configruation
Configuration cfg = new Configuration();
//System.out.println();
//new File("dd").mkdir();
//System.out.println(new File("ftl").getAbsolutePath());
//设置config中加载的模板路径
try {
cfg.setClassForTemplateLoading(TestFreeMarker.class, "/hello/ftl");
//获取模板文件,用于已经设置默认路径是/ftl
//此时hello.ftl就是ftl下的模板文件
Template temp = cfg.getTemplate("hello.ftl");
//创建数据文件,使用map来进行设置
Map<String,Object> root = new HashMap<String,Object>();
root.put("username", "校长");
//通过模板和文件输出相应的数据
temp.process(root, new PrintWriter(System.out));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- Linux常见的指令集
- 浅谈测试用例
- 转的 Service详解
- hdu 5597GTW likes function(欧拉函数)
- [技术无关]
- 浅谈8种基础排序
- WCF技术简介
- openstack 命令行手册
- Access数据库查询
- POJ-3300(用例通过,但不知什么原因一直WA)
- Nutch下载及构建
- hack兼容浏览器
- AngularJS 字符串
- 计算两个日期相差多少天!
- [javase学习笔记]-5.1 数组的定义
- TCP和UDP的区别
- Android开发ListView使用OnScrollListener实现分页加载数据
- 【内存对齐(一)】#pragma pack的用法及大小的计算(转)
- MySQL开启远程登录权限
- FLASH和EEPROM的区别