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

}
}

时间: 2024-08-04 16:12:16

生成freemarker静态页面的工具类的相关文章

PHP生成HTML静态页面的方法

从PHP生成HTML静态页面并存储到以年份和月份为名称创建的目录.读取全部数据批量生成,全部生成后弹出提示.可指定批次生成数量,建议不超过800,否则执行速度会有问题. 为jbxue.com网站功能而开发,代码为本人原创,生成速度一般. (出于众所周知的原因,涉及到数据库的数据字段名称做了改动,并且为了代码明晰去掉了参数过滤的部分) 说明:原动态地址为 moban.php?id=1 ,生成后地址为 html/200808/sell_1.html .page.php为分页程序,本博客中有发布. 页

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

/** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.minxinloan.common.utils; import java.security.SecureRandom; import java.util.UUID; /** * 封装各种生成唯一性ID算法的工具类. * @aut

Freemarker生成HTML静态页面

这段时间的工作是做一个网址导航的项目,面向用户的就是一个首页,于是就想到了使用freemarker这个模板引擎来对首页静态化. 之前是用jsp实现,为了避免用户每次打开页面都查询一次数据库,所以使用了jsp的内置对象application,在Controller中将数据都查询出来, 然后放入application,最后在JSP页面使用jstl标签配合EL表达式 将数据遍历出来.这样做是从一定程度上减轻了服务器的压力和页面的响应速度, 但是仍然没有静态页面响应快. 使用Freemarker步骤:

生成随机验证码图片的工具类

package utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.ByteArrayOutpu

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

刚开始不是理解 写完之后 觉得还蛮简单的 分为这几个步骤 1.获取页面模板Html 2.获取数据 3.解析模板和数据,生成静态页Html代码 4.生成静态文件 模板形式是mvc的模式,会mvc 看一下就懂了 主要是第2步和第3步 需要应用下面文件 RazorEngine.dll System.Web.Razor.dll /// <summary> /// 获取页面的Html代码 /// </summary> /// <param name="url">

spring静态注入组件——工具类常用

如果直接用spring注入静态属性,则会报错,提示@Resource annotation is not supported on static fields,如果又一定要通过spring注入bean,可以采用@PostConstruct注解在某个用来初始化的方法上,注入时注入到另一个不是 静态的变量里,然后在初始化方法里面将注入好的变量赋值给静态变量,通过这些操作就给静态变量赋值. [java] view plaincopy package com.inth.base.util; import

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

下载地址:http://htmlagilitypack.codeplex.com/ 1.添加HtmlAgilityPack.dll引用(引用类using HtmlAgilityPack;). 2.简单根据html中input的id获取value代码如下: // 模拟用户请求 WebClient webClient = new WebClient(); webClient.Encoding = System.Text.Encoding.UTF8; string htmlContext = webC

Java生成指定范围内的工具类

/** * 生成[min, max]之间的随机整数 * * @param min 最小整数 * @param max 最大整数 * @return * @author jqlin */ private static int randomInt(int min, int max){ return new Random().nextInt(max)%(max-min+1) + min; } 原文地址:https://www.cnblogs.com/javahr/p/8323588.html

生成4位随机验证码工具类

keyUtil: package com.duocy.util; import java.util.Random; public class keyUtil {public String keyUtil() { String str="ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopasdfghjklzxcvbnm0123456789"; StringBuilder st=new StringBuilder(4); for(int i=0;i<4;i++)