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. shell的if判断

     shell的if判断 2012-03-16 14:53:05 分类: Python/Ruby   1 概要 有时候你需要指定shell脚本中的依靠命令的成功与否来实施不同过程的行为. if 结构允许 ...

  2. poj1192 最优连通子集(树形dp)

    题目链接:poj1192 最优连通子集 求一棵无向树的最大子树和..类似于求最大子段和的办法,树形dp. dp[i][0]:以i为根,不包括 i 结点的子树最大权 dp[i][1]:以i为根,包括 i ...

  3. apche的主配置文件)

    apche的主配置文件conf/httpd.conf(根据个人主机的路径设置,以下仅供参考) 需配置的行号与方法(示列): 172  #ServerName localhost:80 173 Serv ...

  4. const char * char * const

    2 区分 const * 与 * const 的差别 ( 1 ) 指针本身是常量不可变 ( char * ) const  pCount; const (char *)  pCount; ( 2 ) ...

  5. [转]深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)

    以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...

  6. Positive-definite matrix

    In linear algebra, a symmetric n × n real matrix M is said to be positive definite if zTMz is positi ...

  7. this kernel requires an x86-64 CPU, but only detected an i686 CPU. unable to boot - please ues a ker

    http://blog.csdn.net/xiao_cs/article/details/7728529 this kernel requires an x86-64 CPU, but only de ...

  8. 通过top命令发现plymouthd进程cpu负载达到近100% 解决办法

    最近几天一直遇到服务器cpu100%, 通过top命令发现plymouthd进程cpu负载达到近100% 解决方法:打开 /boot/grub/menu.lst , 去掉 “rhgb quiet”这两 ...

  9. C语言基础--进制

    进制表示: // 1.默认就是10进制 ; // 2.在前面加上一个0就代表八进制 ; // %d是以十进制的方式输出一个整数 printf("%d\n", num1); // % ...

  10. 如何重新安装DEDECMS织梦系统

    重装的方法: 1.找到安装目录\install\index.php.bak文件,改名为index.php:   2.删除安装目录\install\install_lock文件: