您的位置:首页 > 其它

Freemarker-2.3.22 Demo - No01_获取模板并直接输出

2015-06-30 16:21 429 查看
package No01_获取模板并直接输出;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class Test {

public static void main(String[] args) throws Exception {

// 设置模板的home目录,默认从此目录获取模板(此设置只需在应用中设置一次)
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
cfg.setDirectoryForTemplateLoading(new File("D:/freemarker_template_home/"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

// 获取模板
Template temp = cfg.getTemplate("No01_获取模板并直接输出.ftl");

// 将模板和数据模型合并
String filename = temp.getName();
filename = filename.substring(0, filename.lastIndexOf(".") + 1) + "html";
FileOutputStream fos = new FileOutputStream(new File("D:/freemarker_output_home/" + filename));
Writer out = new OutputStreamWriter(fos);

temp.process(null, out);
out.flush();
}

}


No01_获取模板并直接输出.ftl


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello world</title>
</head>
<body>
This is a hello world demo.
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: