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);
将模板和配置的变量结合起来,然后生成文件.
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);
将模板和配置的变量结合起来,然后生成文件.
Freemarker生成HTML文件
本文介绍了一个使用Freemarker模板引擎结合特定数据生成HTML文件的方法。通过获取配置、加载模板并填充数据,最终输出指定路径下的HTML文件。
359

被折叠的 条评论
为什么被折叠?



