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. js获取url参数值,js获取其他页面传递而来的值

    index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&...... 静态html文件js读取url参数 根据获取html的参数值控制htm ...

  2. hello word

    开通微博,用于记录在工作中遇到的点滴问题. 2015/08/31

  3. centos apache源码安装过程记录

    1.下载相关源文件 wget http://mirror.bjtu.edu.cn/apache/httpd/httpd-2.4.18.tar.gzwget http://mirrors.hust.ed ...

  4. 【转】浅析python 中__name__ = '__main__' 的作用

    原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...

  5. java写hadoop全局排序

    前言: 一直不会用java,都是streaming的方式用C或者python写mapper或者reducer的可执行程序.但是有些情况,如全排序等等用streaming的方式往往不好处理,于是乎用原生 ...

  6. WCF初探-6:WCF服务配置

    WCF服务配置是WCF服务编程的主要部分.WCF作为分布式开发的基础框架,在定义服务以及定义消费服务的客户端时,都使用了配置文件的方法.虽然WCF也提供硬编程的方式,通过在代码中直接设置相关对象的属性 ...

  7. IOS 核心动画之CAKeyframeAnimation - iBaby

    - IOS 核心动画之CAKeyframeAnimation - 简单介绍 是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation ...

  8. Client默认用户及登录密码(转)

    Client默认用户及登录密码 SAP系统(如ERP.CRM等)安装完成,初始化状态下有若干个客户端(Client).如果是生产系统,一般只有000.001.066等三个Client:如果是IDES系 ...

  9. php max_input_vars限制数组大小

    今天做一个项目需要post2000个数组过去,发现一直只能接到一半,后来发现是max_input_vars显示问题. 修改php.ini里面max_input_vars的大小就可以了

  10. env.sh

    #!/bin/sh#docker exec -i t nginx /usr/share/app/nginx/vip/webapp/evn.sh testtestUrl='http:\/\/192.16 ...