{code}
package com.gbits.wdhw.web;
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.net.URISyntaxException;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import com.gbits.wdhw.common.config.operConfig;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
*
* 生成静态页操作
*
* @author yanse
*
*/
public class produceHtml
{
/**
* 生成静态页面主方法
*
* @param context
* ServletContext
* @param data
* 模版的数据来源,freemarker通过一个Map给ftl模版送数据
* @param templatePath
* ftl模版所在的路径
* @param targetHtmlPath
* 最后生成静态页的路径
*/
public static void crateHTML(ServletContext context,
Map<String, Object> data, String templatePath, String targetHtmlPath)
{
Configuration freemarkerCfg = new Configuration();
// 加载模版
freemarkerCfg.setServletContextForTemplateLoading(context, "/");
freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
try
{
// 指定模版路径
Template template = freemarkerCfg
.getTemplate(templatePath, "UTF-8");
template.setEncoding("UTF-8");
// 静态页面路径
String htmlBashPath = operConfig.readConfigByKey("htmlPath");
String htmlPath = context.getRealPath(htmlBashPath) + "/"
+ targetHtmlPath;
File htmlFile = new File(htmlPath);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "UTF-8"));
// 处理模版
template.process(data, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* templatePath模板文件存放路径,templateName 模板文件名称,filename 生成的文件名称
*
* @param templatePath
* @param templateName
* @param fileName
* @param root
*/
public static void analysisTemplate(String templatePath,
String templateName, String fileName, Map<String, Object> data)
{
try
{
// 初使化FreeMarker配置
Configuration config = new Configuration();
// 设置要解析的模板所在的目录,并加载模板文件
config.setDirectoryForTemplateLoading(new File(templatePath));
// 设置包装器,并将对象包装为数据模型
config.setObjectWrapper(new DefaultObjectWrapper());
// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
// 否则会出现乱码
Template template = config.getTemplate(templateName, "UTF-8");
// 合并数据模型与模板
FileOutputStream fos = new FileOutputStream(fileName);
Writer out = new OutputStreamWriter(fos, "UTF-8");
template.process(data, out);
out.flush();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (TemplateException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
System.out.println(produceHtml.class.getClassLoader()
.getResource("/wdhw").toURI().getPath());
}
catch (URISyntaxException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* 根据模块标识生成对应的模块页面
*
* @param context
* @param data
* @param moderFlag
* @param id
*/
public static void createModerHTML(ServletContext context,
Map<String, Object> data, String moderFlag, String id)
{
String templatePath = produceHtml.getTemplateNameByType(moderFlag);
String targetHtmlPath = produceHtml.getHtmlPathByType(moderFlag, id);
createFolder(context.getContextPath() + "/html/"+moderFlag,moderFlag);
produceHtml.crateHTML(context, data, templatePath, targetHtmlPath);
}
/**
*
* 判断文件夹是否存在,不存在则创建一个
*/
public static void createFolder(String path,String moderFlag)
{
if (!moderFlag.equals("index"))
{
File file = new File(path);
// 判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists())
{
file.mkdir();
}
}
}
public static void testHTML()
{
}
/**
*
* 根据类型获取模板
*
* @param type
* @return
*/
public static String getTemplateNameByType(String type)
{
String templatePath = operConfig.readConfigByKey("templatePath");
return templatePath + type + "/" + type + ".ftl";
}
/**
*
* 根据类型获取最后生成静态页的路径
*
* @param type
* @param id
* @return
*/
public static String getHtmlPathByType(String type, String id)
{
String htmlPath = "";
if (type.equals("index"))
{
htmlPath = type + ".html";
} else
{
htmlPath = type + "/" + id + ".html";
}
return htmlPath;
}
}
{code}
package com.gbits.wdhw.web;
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.net.URISyntaxException;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import com.gbits.wdhw.common.config.operConfig;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
*
* 生成静态页操作
*
* @author yanse
*
*/
public class produceHtml
{
/**
* 生成静态页面主方法
*
* @param context
* ServletContext
* @param data
* 模版的数据来源,freemarker通过一个Map给ftl模版送数据
* @param templatePath
* ftl模版所在的路径
* @param targetHtmlPath
* 最后生成静态页的路径
*/
public static void crateHTML(ServletContext context,
Map<String, Object> data, String templatePath, String targetHtmlPath)
{
Configuration freemarkerCfg = new Configuration();
// 加载模版
freemarkerCfg.setServletContextForTemplateLoading(context, "/");
freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
try
{
// 指定模版路径
Template template = freemarkerCfg
.getTemplate(templatePath, "UTF-8");
template.setEncoding("UTF-8");
// 静态页面路径
String htmlBashPath = operConfig.readConfigByKey("htmlPath");
String htmlPath = context.getRealPath(htmlBashPath) + "/"
+ targetHtmlPath;
File htmlFile = new File(htmlPath);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "UTF-8"));
// 处理模版
template.process(data, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* templatePath模板文件存放路径,templateName 模板文件名称,filename 生成的文件名称
*
* @param templatePath
* @param templateName
* @param fileName
* @param root
*/
public static void analysisTemplate(String templatePath,
String templateName, String fileName, Map<String, Object> data)
{
try
{
// 初使化FreeMarker配置
Configuration config = new Configuration();
// 设置要解析的模板所在的目录,并加载模板文件
config.setDirectoryForTemplateLoading(new File(templatePath));
// 设置包装器,并将对象包装为数据模型
config.setObjectWrapper(new DefaultObjectWrapper());
// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
// 否则会出现乱码
Template template = config.getTemplate(templateName, "UTF-8");
// 合并数据模型与模板
FileOutputStream fos = new FileOutputStream(fileName);
Writer out = new OutputStreamWriter(fos, "UTF-8");
template.process(data, out);
out.flush();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (TemplateException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
System.out.println(produceHtml.class.getClassLoader()
.getResource("/wdhw").toURI().getPath());
}
catch (URISyntaxException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* 根据模块标识生成对应的模块页面
*
* @param context
* @param data
* @param moderFlag
* @param id
*/
public static void createModerHTML(ServletContext context,
Map<String, Object> data, String moderFlag, String id)
{
String templatePath = produceHtml.getTemplateNameByType(moderFlag);
String targetHtmlPath = produceHtml.getHtmlPathByType(moderFlag, id);
createFolder(context.getContextPath() + "/html/"+moderFlag,moderFlag);
produceHtml.crateHTML(context, data, templatePath, targetHtmlPath);
}
/**
*
* 判断文件夹是否存在,不存在则创建一个
*/
public static void createFolder(String path,String moderFlag)
{
if (!moderFlag.equals("index"))
{
File file = new File(path);
// 判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists())
{
file.mkdir();
}
}
}
public static void testHTML()
{
}
/**
*
* 根据类型获取模板
*
* @param type
* @return
*/
public static String getTemplateNameByType(String type)
{
String templatePath = operConfig.readConfigByKey("templatePath");
return templatePath + type + "/" + type + ".ftl";
}
/**
*
* 根据类型获取最后生成静态页的路径
*
* @param type
* @param id
* @return
*/
public static String getHtmlPathByType(String type, String id)
{
String htmlPath = "";
if (type.equals("index"))
{
htmlPath = type + ".html";
} else
{
htmlPath = type + "/" + id + ".html";
}
return htmlPath;
}
}
{code}