package cn.bocai.pc.util;

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;

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,"GBK");
t.setEncoding("UTF-8");
// 如果根路径存在,则递归创建子目录
this.creatDirs(htmlFilePath);
File afile = new File(htmlFilePath + "/" + htmlFileName);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(afile),"UTF-8"));
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());
}
}

/**
* 模板生成静态html的方法
* @param templateFileName(模板文件名)
* @param templateFilePath(指定模板目录)
* @param contextMap (用于处理模板的属性Object映射)
* @param htmlFilePath(指定生成静态html的目录)
* @param htmlFileName(生成的静态文件名)
*/
@SuppressWarnings("unchecked")
public void geneHtmlFileUTF8(String templateFileName, String templateFilePath, Map contextMap,
String htmlFilePath, String htmlFileName) {

try {
Template t = this.getFreeMarkerCFG(templateFilePath).getTemplate(templateFileName,"UTF-8");
t.setEncoding("UTF-8");
// 如果根路径存在,则递归创建子目录
this.creatDirs(htmlFilePath);
File afile = new File(htmlFilePath + "/" + htmlFileName);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(afile),"UTF-8"));
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();
this.config.setDefaultEncoding("UTF-8");
try {
this.config.setDirectoryForTemplateLoading(new File(
templateFilePath));
} catch (Exception ex) {
throw ex;
}
}
return this.config;
}

public void geneHtmlFileUTF8(String templateFileName, String templateFilePath, Map contextMap,String htmlFilePath) {
try {
Template t = this.getFreeMarkerCFG(templateFilePath).getTemplate(templateFileName,"UTF-8");
t.setEncoding("UTF-8");
// 如果根路径存在,则递归创建子目录
this.creatDirs(htmlFilePath);
File afile = new File(htmlFilePath);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(afile),"UTF-8"));
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静态页面的工具类的更多相关文章

  1. Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用

    现在,在不少应用场合中都希望做到数据抓取,特别是基于网页部分的抓取.其实网页抓取的过程实际上是通过编程的方法,去抓取不同网站网页后,再进行 分析筛选的过程.比如,有的比较购物网站,会同时去抓取不同购物 ...

  2. 编写Java程序,使用单例模式,创建可以生成银联借记卡号的工具类,银联借记卡号是一个 19 位的数字,卡号以“62”开头,如图所示。

    查看本章节 查看作业目录 需求说明: 使用单例模式,创建可以生成银联借记卡号的工具类,银联借记卡号是一个 19 位的数字,卡号以"62"开头,如图所示. 实现思路: (1)创建 J ...

  3. 封装各种生成唯一性ID算法的工具类

    /** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 ( ...

  4. Freemarker生成HTML静态页面

    这段时间的工作是做一个网址导航的项目,面向用户的就是一个首页,于是就想到了使用freemarker这个模板引擎来对首页静态化. 之前是用jsp实现,为了避免用户每次打开页面都查询一次数据库,所以使用了 ...

  5. Excel生成Oracle数据库表sql工具类

    1.解决问题: 开发文档中字段比较多的时候,建表sql(Oracle下划线命名规范)比较麻烦,容易出错~~ (主要是懒) 特意手写一个工具,根据excel字段,生成建表的sql语句. ~~~末尾附Gi ...

  6. java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)

    一.Scanner类 1.api简介: 应用程序编程接口 2.Scanner类: 作用:获取键盘输入的数据 位置: java.util.Scanner. 使用:使用成员方法nextInt() 和 ne ...

  7. asp .net 模板引擎 使用 Razor 生成html静态页面

    刚开始不是理解 写完之后 觉得还蛮简单的 分为这几个步骤 1.获取页面模板Html 2.获取数据 3.解析模板和数据,生成静态页Html代码 4.生成静态文件 模板形式是mvc的模式,会mvc 看一下 ...

  8. 处理html页面元素工具类(HtmlAgilityPack.dll)的使用

    下载地址:http://htmlagilitypack.codeplex.com/ 1.添加HtmlAgilityPack.dll引用(引用类using HtmlAgilityPack;). 2.简单 ...

  9. PHP生成HTML静态页面。

    function Generate(){ $html = '<!DOCTYPE html><html lang="en"><head> < ...

随机推荐

  1. NGUI Scroll List

    NGUI Scroll List 1.Add GameObject with Script UI Panel(NGUI -> UI -> NGUI Panel) and Script UI ...

  2. phpstormn 中 xdebug 的详细配置2

    配置PHPStorm 图1:首先配置PHP解释器的路径 图2:File>Settings>PHP>Servers,这里要填写服务器端的相关信息,name填localhost,host ...

  3. html中input文本框,初始里边有文字提示,当点击时,文字消失,怎么设置?

    使用onfocus事件检查当前值,如果是默认值,就将value属性置空.如:<input type="text" value="请输入内容" onfocu ...

  4. 【59测试】【树】【dp】

    第一题 A : 这棵树由n个节点以及n - 1条有向边构成,每条边都从父亲节点指向儿子节点,保证除了根节点以外的每个节点都有一个唯一的父亲.树上的节点从1到n标号.该树的一棵子树的定义为某个节点以及从 ...

  5. SparkSQL之旅

    1.准备数据employee.txt ,Gong Shaocheng, ,Li Dachao, ,Qiu Xin, ,Cheng Jiangzhong, ,Wo Binggang, 将数据放入hdfs ...

  6. Ubuntu gmake: command not found

    由于ubuntu上取消了gmake(GUNmake)的而全部使用make代替.所以此问题的解决方式是有两种: 1.makefile中 gmake用make代替: 2.创建一个make的gmake连接: ...

  7. Visual Studio 2015 社区版.专业版.企业版[含安装密钥Pro&Ent]

    社区版(Visual Studio Community 2015)可供非企业或开源开发者们免费访问: 在线安装exe:http://download.microsoft.com/download/B/ ...

  8. libdispatch for Linux

    这个Dispatch是苹果的一个高效的处理库,它在ubuntu上的安装如下: Build/Runtime Requirements 如下: libBlocksRuntime libpthread_wo ...

  9. json对象转换为json字符串

    今天浏览网页的时候看到这个题也是一道很经典的题目了 var str ='aaaaaaajsdjdfkdkg'; ,]; var obj={}; ;i<str.length;i++){ if(!o ...

  10. HDFS中的checkpoint( 检查点 )的问题

    1.问题的描述 由于某种原因,需要在原来已经部署了Cloudera CDH集群上重新部署,重新部署之后,启动集群,由于Cloudera Manager 会默认设置dfs.namenode.checkp ...