静态页面生成

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.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerUtil {
	private static Configuration config = null;

	/** 初始化属性文件 */
	public static ResourceBundle properties = GetProperties.getPropertiesValue("file");

	/**
	 * 如果目录不存在,则自动创建
	 * 
	 * @param path
	 * @return boolean 是否成功
	 */
	private static boolean creatDirs(String path) {
		File aFile = new File(path);
		if (!aFile.exists()) {
			return aFile.mkdirs();
		} else {
			return true;
		}
	}

	public static void main(String[] args) {
		Map<String, Object> contextMap = new HashMap<String, Object>();
		contextMap.put("title", "沪指跌0.63%失守3100点");
		contextMap.put("sourse", "*****");
		contextMap.put("times", "09:26 14:20");

		geneHtmlFile(contextMap, "article4.html", properties.getString("articlePath"), "147.html");
	}

	/**
	 * 模板生成静态html的方法
	 * 
	 * @author zhy
	 * @param templateFileName (模板文件名)
	 * @param templateFilePath (指定模板目录)
	 * @param contextMap (用于处理模板的属性Object映射)
	 * @param htmlFilePath (指定生成静态html的目录)
	 * @param htmlFileName (生成的静态文件名)
	 */
	public static void geneHtmlFile(Map<String, Object> contextMap, String htmlFileName, 
			String modulePath, String moduleName) {
		try {
			Template t = getFreeMarkerCFG(modulePath).getTemplate(moduleName);
			// 如果根路径存在,则递归创建子目录
			creatDirs(modulePath);
			File afile = new File(modulePath + "/" + htmlFileName);
			FileOutputStream fileInput = new FileOutputStream(afile);
			
			OutputStreamWriter outWrite = new OutputStreamWriter(fileInput, "utf-8");
			
			Writer out = new BufferedWriter(outWrite);
			
			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获取.
	 * 
	 * @author zhy
	 * @param templateFilePath 获取模板路径
	 * @return Configuration 返回freemaker的配置属性
	 * @throws Exception
	 */
	private static Configuration getFreeMarkerCFG(String templateFilePath) throws Exception {
		if (null == config) {
			config = new Configuration(Configuration.getVersion());
			try {
				config.setDirectoryForTemplateLoading(new File(templateFilePath));
				config.setObjectWrapper(new DefaultObjectWrapper(Configuration.getVersion()));  
				config.setDefaultEncoding("UTF-8");
			} catch (Exception ex) {
				throw ex;
			}
		}
		return config;
	}
}

获取属性文件

import java.util.ResourceBundle;

public class GetProperties {
	/**
	 * 根据属性文件的位置获取属性文件
	 * 
	 * @author zhy
	 * @param propertiesPosition 属性文件的位置
	 * @return
	 */
	public static ResourceBundle getPropertiesValue(String propertiesPosition) {
		return ResourceBundle.getBundle(propertiesPosition);
	}
	
	/**
	 * 根据属性文件的位置获取属性文件中的值
	 * 
	 * @author zhy
	 * @param propertiesPosition 属性文件的位置
	 * @param propertieName 要读取的属性文件中的属性
	 * @return
	 */
	public static Object getPropertiesValue(String propertiesPosition, String propertieName) {
		return ResourceBundle.getBundle(propertiesPosition).getObject(propertieName);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值