`

通过模板和配置文件生成系统文件

 
阅读更多


public void buildHtml(String templateFilePath, String htmlFilePath, Map<String, Object> data) {
try {
ServletContext servletContext = ServletActionContext.getServletContext();
Configuration configuration = freemarkerManager.getConfiguration(servletContext);
Template template = configuration.getTemplate(templateFilePath);
File htmlFile = new File(servletContext.getRealPath(htmlFilePath));
File htmlDirectory = htmlFile.getParentFile();
if (!htmlDirectory.exists()) {
htmlDirectory.mkdirs();
}
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
template.process(data, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

templateFilePath : freemaker 的模板文件
htmlFilePath : 要生成的文件
通过
Template template = configuration.getTemplate(templateFilePath);
将模板和配置的变量结合起来,然后生成文件.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics