FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写
FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用程序
虽然FreeMarker具有一些编程的能力,但通常由Java程序准备要显示的数据,由FreeMarker生成页面,通过模板显示准备的数据.
生成BT下载网站:http://www.bt285.cn,同样的:我也可以生成 http://www.tudousee.cn
java 代码:
/**
*生成BT下载网站:http://www.bt285.cn,同样的:我也可以生成 http://www.tudousee.cn
*/
public void createFtl(Map<String, Object> req){
try {
Configuration cfg = new Configuration();
// - Templates are stoted in the WEB-INF/templates directory of the
// Web app.
String tempate = FileConstent.PrePath + "/tempates/";
String destPath = tempate +"index/www.bt285.cn_index.html";
String destPathSpider = tempate +"index/www.bt285.cn_index_spider.html";
// new File(destPath).deleteOnExit();
// new File(destPathSpider).deleteOnExit();
cfg.setDirectoryForTemplateLoading(new File(tempate));
Template tIndex = getTemplate(cfg,
"index/http://www.bt285.cn_index.ftl",Locale.ENGLISH,Constent.DefaultEncodIng);
Template tIndexSpider = getTemplate(cfg,
"index/http://www.bt285.cn_index_spider.ftl",Locale.ENGLISH,Constent.DefaultEncodIng);
doWirte(tIndex,destPath,req);
doWirte(tIndexSpider,destPathSpider,req);
} catch (Exception e) {
e.printStackTrace();
}
}
public void doWirte(Template t,String path,Map<String, Object> req){
try {
//BufferedOutputStream bw = null;
// FileOutputStream outStream = new FileOutputStream(path);
//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream,Constent.DefaultEncodIng));
BufferedWriter out = new BufferedWriter(new FileWriter(path));
// Merge the data-model and the template
t.setEncoding(Constent.DefaultEncodIng);
t.process(req,out);
} catch (Exception e) {
e.printStackTrace();
}
}
private WNewsDAO<WNews> newsDAO;
private WCommentsDAO commentsDAO;
public static Template getTemplate(Configuration cfg,String name,Locale locale,String decode)
throws IOException{
return cfg.getTemplate(name);
}