Freemaker生成静态html页面
FreeMarker 是一个用Java编写的模板引擎,主要用来生成HTML Web页面,特别是基于MVC模式的应用程序。虽然FreeMarker具有一些编程的能力,但不像PHP,通常由Java程序准备要显示的数据,由 FreeMarker模板生成页面。 FreeMarker可以作为Web应用框架一个组件,但它与容器无关,在非Web应用程序环境也能工作的很好。 FreeMarker适合作为MVC的视图组件,还能在模板中使用JSP标记库。
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Map;import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;/*** freemarker生成静态html* @author lpz**/ public class GeneratorHtml {private Configuration config = null; /** * 如果目录不存在,则自动创建* @param path * @return boolean 是否成功 */ private boolean creatDirs(String path) { File aFile = new File(path); if (!aFile.exists()) { return aFile.mkdirs(); } else { return true; } } /*** 模板生成静态html的方法* @param templateFileName(模板文件名)* @param templateFilePath(指定模板目录)* @param contextMap (用于处理模板的属性Object映射)* @param htmlFilePath(指定生成静态html的目录)* @param htmlFileName(生成的静态文件名)*/@SuppressWarnings("unchecked") public void geneHtmlFile(String templateFileName, String templateFilePath, Map contextMap, String htmlFilePath, String htmlFileName) { try { Template t = this.getFreeMarkerCFG(templateFilePath).getTemplate(templateFileName); // 如果根路径存在,则递归创建子目录 this.creatDirs(htmlFilePath); File afile = new File(htmlFilePath + "/" + htmlFileName); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(afile))); t.process(contextMap, out); out.flush(); out.close(); } catch (TemplateException e) { System.out.print(e.getMessage()); } catch (IOException e) { System.out.print(e.getMessage()); } catch (Exception e) { System.out.print(e.getMessage()); } } /** * * 获取freemarker的配置,freemarker本身支持classpath,目录或从ServletContext获取. * * @param templateFilePath * 获取模板路径 * @return Configuration 返回freemaker的配置属性 * @throws Exception */ private Configuration getFreeMarkerCFG(String templateFilePath) throws Exception { if (null == this.config) { this.config = new Configuration(); try { this.config.setDirectoryForTemplateLoading(new File( templateFilePath)); } catch (Exception ex) { throw ex; } } return this.config; } }
Freemaker生成静态html页面相关推荐
- 生成html页面的ftl文件,FreeMarker生成静态HTML页面的工具类FreeMarkerUtil
FreeMarker生成静态HTML页面的工具类FreeMarkerUtil 一.FreeMarkerUtil工具类: import com.huaxia.entity.News; import co ...
- smarty能创建 php页面,smarty 生成静态html页面
生成静态页面,用php生成会比smarty生成要快,但是如果你用smarty模板做的页面,现在要生成html的话,就可以看看这篇smarty 生成静态html页面教程物.<?php $tpl = ...
- 利用Java生成静态HMTL页面的方法收集
利用Java生成静态HMTL页面的方法收集 生成静态页面技术解决方案之一 转载者前言:这是一个全面的jsp动态页面静态化方案,本站的帖子静态化方案将借鉴这篇帖子中方法.向[url]http://www ...
- php动态生成页面结构,PHP 动态生成静态HTML页面示例代码
temp.html {penglig_site_title} test.php header('content-type:text/html; charset=utf-8');//防止生成的页面乱码 ...
- freeMarker生成静态html页面
yml配置freemarker freemarker:request-context-attribute: req #req访问requestsuffix: .ftl #后缀名content-type ...
- 通过xml 生成html页面,使用xmldom在服务器端生成静态html页面
服务器端的生成的页面数据,为了提高访问速度,往往需要生成静态的htm页面. 通常,可以使用fso生成静态的htm页. 但如果是fso被禁止或没有使用fso的权限,就需要其他的方法来解决. 利用xmld ...
- 记一次在nuxt中使用generate批量生成静态站点页面
做过seo的同学都知道,一些不是实时变化的页面(比如一个星期更新一次内容)我们可以生成静态的站点去让爬虫去爬去,这样就无需一次次地调用接口了. 那么在nuxt中我们怎么去做呢? nuxt为我们提供了一 ...
- JSP生成静态HTML页面的实践方法和设计思路
我们以两个大型网站为例作比较: 51job和智联招聘(先声明我不是为他们作广告,仅以他们为例作技术上的比较) 51job采用的是比较"先进"的php技术,而智联用的是比较落后的as ...
- 将给定数据源生成静态HTML页面持久化到项目之外的硬盘
一.java代码 设置好数据源map Map<String,String> map=new HashMap<>();map.put("knowledgeName&qu ...
最新文章
- Hibernate 乐观锁和悲观锁
- 大数据与 AI 生态中的开源技术总结
- linux tomcat自启动设置
- Centos6.8上httpd配置腾讯云SSL证书
- 设计php框架_自制PHP框架之设计模式
- 【面试收藏】Redis最全面试题
- HIve map jion的原理、操作和使用场景
- python:python对象属性及属性函数property()
- Codeforces914D Bash and a Tough Math Puzzle (思维 线段树)
- 近40年码龄,从通宵写代码到三思而后行——专访云风
- mate7 刷机 android 7,华为Mate7如何刷机 华为Mate7详细root刷机教程
- sql cai bird教程学习记录
- 【毕业设计】大数据工作岗位数据分析与可视化 - python flask
- 【绝知此事要躬行】线性表之链表OJ(下)
- spacy和en安装
- linux2t硬盘格式化时间,Linux运维知识:linux下大于2T硬盘格式化方法
- python中turtle画笑脸_如何用python画笑脸
- Flutter安装详解 as版本
- 操作系统指纹探测实验
- 获取android 默认的机器人图标,APP桌面只显示默认机器人图标的几个解决方法