Freemarker简单用法
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
/**
* Freemarker最简单的例子
*
* @author leizhimin 11-11-17 上午10:32
*/
public class Test2 {
public static void main(String[] args) throws Exception{
//创建一个模版对象
Template t = new Template(null, new StringReader("用户名:${user};URL: ${url};姓名: ${name}"), null);
//创建插值的Map
Map map = new HashMap();
map.put("user", "lavasoft");
map.put("url", "http://www.baidu.com/");
map.put("name", "百度");
//执行插值,并输出到指定的输出流中
t.process(map, new OutputStreamWriter(System.out));
}
}
Process finished with exit code 0
import freemarker.template.Template;
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
/**
* Freemarker最简单的例子
*
* @author leizhimin 11-11-14 下午2:44
*/
public class Test {
private Configuration cfg; //模版配置对象
public void init() throws Exception {
//初始化FreeMarker配置
//创建一个Configuration实例
cfg = new Configuration();
//设置FreeMarker的模版文件夹位置
cfg.setDirectoryForTemplateLoading(new File("G:\\testprojects\\freemarkertest\\src"));
}
public void process() throws Exception {
//构造填充数据的Map
Map map = new HashMap();
map.put("user", "lavasoft");
map.put("url", "http://www.baidu.com/");
map.put("name", "百度");
//创建模版对象
Template t = cfg.getTemplate("test.ftl");
//在模版上执行插值操作,并输出到制定的输出流中
t.process(map, new OutputStreamWriter(System.out));
}
public static void main(String[] args) throws Exception {
Test hf = new Test();
hf.init();
hf.process();
}
}
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<p>Our latest product:
<a href="${url}">${name}</a>!
</body>
</html>
尊敬的用户你好:
用户名:${user};
URL: ${url};
姓名: ${name}
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome lavasoft!</h1>
<p>Our latest product:
<a href="http://www.baidu.com/">百度</a>!
</body>
</html>
尊敬的用户你好:
用户名:lavasoft;
URL: http://www.baidu.com/;
姓名: 百度
Process finished with exit code 0
Freemarker简单用法的更多相关文章
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- [转]Valgrind简单用法
[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...
- Oracle的substr函数简单用法
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- WPF之Treeview控件简单用法
TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
随机推荐
- 在apache虚拟目录配置
在apache虚拟目录配置中 <VirtualHost *:80>xxx xxx xxx</VirtualHost> 不能写成 <VirtualHost *>xxx ...
- 日记整理---->2017-05-14
学习一下知识吧,好久没有写博客了.如果他总为别人撑伞,你又何苦非为他等在雨中. 学习的知识内容 一.关于base64的图片问题 byte[] decode = Base64.base64ToByteA ...
- C++ 操作符new和delete
参考资料: http://en.cppreference.com/w/cpp/memory/new/operator_new http://en.cppreference.com/w/cpp/memo ...
- Android.mk(3) 宏
https://www.jianshu.com/p/7c20b299ee63 传统上我们一直称这种东西为makefile中的变量,其实本质上就是一个宏,只是做的是字符串替换.我们何如就把它叫做宏呢. ...
- SPOJ1007 VLATTICE - Visible Lattice Points
VLATTICE - Visible Lattice Points no tags Consider a N*N*N lattice. One corner is at (0,0,0) and th ...
- Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapinfo' (aka) 'enum CGBitmapInfo')
The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo ty ...
- 深入浅出WPF之Binding的使用(一)
在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象:这 ...
- [转]Shell脚本之无限循环的两种方法
方法一: while循环,用的比较多的 #!/bin/bash set j= while true do let "j=j+1" echo "----------j is ...
- Android sd卡log日志
import android.os.Environment; import android.util.Log; import java.io.File; import java.io.FileOutp ...
- python---不支持中文注释解决办法
很神奇的一件事儿,pycharm不支持中文注释,具体解决办法: #-*- coding: utf- -*- 具体使用: