打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java利用freemarker生成html静态页面
提升网站性能的方式有很多,例如有效的使用缓存,生成静态页面等等。今天要说的就是生成静态页面的方式。使用html静态页面是为了保证最快的反应速度,毕竟html 比jsp少了一层服务器执行.速度上要快的多
FreeMarker 是一个用java编写的模版引擎,主要用来生成web html页面,通常由java程序准备要显示的数据,与FreeMarker 生成静态页面.
编写ftl模版可以生成html代码,必须导入freemarker包
核心代码:
private Configuration tempConfiguration;
public void setTempConfiguration(Configuration tempConfiguration) {
this.tempConfiguration = tempConfiguration;
}
public static void crateHTML(ServletContext context,
Map<String, Object> data, String templatePath, String targetHtmlPath) {
try {
//filepath:ftl存放路径(/template/file/static)
this.tempConfiguration.setDirectoryForTemplateLoading(new File(filePath));
//templatePath:ftl文件名称(template.ftl)
Template template = this.tempConfiguration.getTemplate(templatePath);
template.setEncoding("UTF-8");
// 静态页面要存放的路径
String htmlPath = targetHtmlPath;
File htmlFile = new File(htmlPath);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "UTF-8"));
// 处理模版 map数据 ,输出流
template.process(data, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
FreeMarker如何生成静态页面
Struts2第十七课:Struts2中使用FreeMarker充当表现层_李飞虎 jav...
Java Web开发——FreeMarker模板使用方法讲解
freeMarker模板引擎将表格中的数据导出成Excel
freemarker的一个例子
​SpringBoot整合freemarker、thymeleaf模版引擎
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服