日期:2014-05-17  浏览次数:20697 次

Freemarker生成静态html示例
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.Locale;
import java.util.Map;

import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.JspSupportServlet;
import org.apache.struts2.views.freemarker.FreemarkerManager;

import com.opensymphony.xwork2.ActionSupport;
import com.sz7road.web.common.util.ServiceLocator;
import com.sz7road.web.service.TempHtmlService;

import freemarker.ext.jsp.TaglibFactory;
import freemarker.ext.servlet.HttpRequestHashModel;
import freemarker.ext.servlet.ServletContextHashModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TempHtmlAction extends ActionSupport {

	private static final Logger logger = LogManager.getLogger(GeneHtmlFileAction.class);
	
	private Configuration freemarker_cfg = null;
	
	public String tempIndixHtml(){
		TempHtmlService tempHtmlService = ServiceLocator.getTempHtmlrService();
		String sGeneFilePath = "";
		String sFileName = "index.html";
		try {
			Map<String, Object> dataMap = tempHtmlService.createIndex();
			geneHtmlFile("temp/index.ftl", dataMap, sGeneFilePath, sFileName);
		} catch (Exception e) {
			logger.error("Index Html Error:" + e.getMessage(), e);
		}
		return SUCCESS;
	}	
	/**
	 * 获取freemarker的配置. freemarker本身支持classpath,目录和从ServletContext获取.
	 */
	protected Configuration getFreeMarkerCFG() {
		if (null == freemarker_cfg) {
			File templateFile = new File(ServletActionContext
					.getServletContext().getRealPath("/") + "WEB-INF/template");
			freemarker_cfg = new Configuration();
			try {
				freemarker_cfg.setDirectoryForTemplateLoading(templateFile);
				freemarker_cfg.setEncoding(Locale.CHINA, "UTF-8");
			} catch (IOException e) {
				logger.error("Get tempalte error", e);
			}
		}

		return freemarker_cfg;
	}

	/**
	 * 生成静态文件.
	 * 
	 * @param templateFileName
	 *            模板文件名,相对htmlskin路径,例如"/tpxw/view.ftl"
	 * @param propMap
	 *            用于处理模板的属性Object映射
	 * @param htmlFilePath
	 *            要生成的静态文件的路径,相对设置中的根路径,例如 "/tpxw/1/2005/4/"
	 * @param htmlFileName
	 *            要生成的文件名,例如 "1.htm"
	 */
	public boolean geneHtmlFile(String templateFileName,
			Map<String, Object> propMap, String htmlFilePath,
			String htmlFileName) {
		// @todo 从配置中取得要静态文件存放的根路径:需要改为自己的属性类调用
		//String sRootDir = "/var/www/ddtank/web";
		String sRootDir = ServletActionContext.getRequest().getRealPath("/webpage");
		try {
			if (propMap == null) {
				propMap = new HashMap<String, Object>();
			}
			ServletContext servletContext = ServletActionContext
					.getServletContext();
			// data.put("JspTaglibs", new TaglibFactory(servletContext));
			FreemarkerManager freemarkerManager = new FreemarkerManager();
			Configuration configuration = freemarkerManager
					.getConfiguration(servletContext);
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			ServletContextHashModel servletContextModel = (ServletContextHashModel) servletContext
					.getAttribute(".freemarker.Application");
			if (servletContextModel == null) {
				GenericServlet servlet = JspSupportServlet.jspSupportServlet;
				if (servlet != null) {
					servletContextModel = new ServletContextHashModel(servlet,
							configuration.getObjectWrapper());
					servletContext.setAttribute(".freemarker.Application",
							servletContextModel);
					TaglibFactory taglibs = new TaglibFactory(servletContext);
					servletContext.setAttribute(".freema