zoukankan      html  css  js  c++  java
  • 生成freemarker静态页面的工具类

    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());
    }

    }
    }

  • 相关阅读:
    Windows XP下 Android开发环境 搭建
    Android程序的入口点
    在eclipse里 新建android项目时 提示找不到proguard.cfg
    64位WIN7系统 下 搭建Android开发环境
    在eclipse里 新建android项目时 提示找不到proguard.cfg
    This Android SDK requires Android Developer Toolkit version 20.0.0 or above
    This Android SDK requires Android Developer Toolkit version 20.0.0 or above
    Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead
    Windows XP下 Android开发环境 搭建
    Android程序的入口点
  • 原文地址:https://www.cnblogs.com/swite/p/6230698.html
Copyright © 2011-2022 走看看