1.下载freemarker,解压,在工程中加入freemarker.jar
2.创建模本文件 myftl.ftl
3.使用模板,生成新文件
2.创建模本文件 myftl.ftl
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<p>点一下:
<a href="${websites.url}">${websites.name}</a>!
</body>
</html>
3.使用模板,生成新文件
public class Make {
public static void main(String[] args) {
Configuration cfg = new Configuration();
Template template = null;
try {
template = cfg.getTemplate("src/test/myftl.ftl");
// cfg.setServletContextFromTemplateLoading(getServletContext,"WEB-INF/test.ftl");
} catch (IOException e) {
e.printStackTrace();
}
Map<String,Object> root = new HashMap<String,Object>();
Map<String,Object> websites = new HashMap<String,Object>();
root.put("user", "juju");
root.put("websites", websites);
websites.put("url", "http://www.google.com");
websites.put("name", "谷歌");
//输出到控制台
//Writer w = new OutputStreamWriter(System.out);
//输出到文件
Writer w = null;
try {
w = new FileWriter("src/test/output.htm");
} catch (IOException e1) {
e1.printStackTrace();
}
try {
template.process(root, w);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}