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(".freemarker.JspTaglibs", taglibs); } } HttpRequestHashModel requestModel = (HttpRequestHashModel) request .getAttribute(".freemarker.Request"); if ((requestModel == null) || (requestModel.getRequest() != request)) { requestModel = new HttpRequestHashModel(request, response, configuration.getObjectWrapper()); request.setAttribute(".freemarker.Request", requestModel); } propMap.put("Application", servletContextModel); propMap.put("Request", requestModel); propMap.put("Session", ServletActionContext.getRequest() .getSession()); propMap.put("JspTaglibs", new TaglibFactory(ServletActionContext.getServletContext())); Template t = getFreeMarkerCFG().getTemplate(templateFileName); // 如果根路径存在,则递归创建子目录 creatDirs(sRootDir, htmlFilePath); File afile = new File(sRootDir + "/" + htmlFilePath + "/" + htmlFileName); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(afile), "UTF-8")); t.process(propMap, out); out.flush(); out.close(); } catch (TemplateException e) { logger.error("Error while processing FreeMarker template " + templateFileName, e); return false; } catch (IOException e) { logger.error("Error while generate Static Html File " + htmlFileName, e); return false; } return true; } /** * 创建多级目录 * * @param aParentDir * String * @param aSubDir * 以 / 开头 * @return boolean 是否成功 */ public static boolean creatDirs(String aParentDir, String aSubDir) { File aFile = new File(aParentDir); if (!aFile.exists()) { aFile.mkdirs(); } File aSubFile = new File(aParentDir + aSubDir); if (!aSubFile.exists()) { return aSubFile.mkdirs(); } else { return true; } } }?
详细解决方案
Freemarker生成静态html示范
热度:265 发布时间:2012-06-30 17:20:12.0
相关解决方案
- freemarker 多个list嵌套运用
- freemarker 语法相干!
- Struts2+Spring+JPA+FREEMARKER 登录程序异常
- 访问Tomcat的url的时候如何自动调用index.html
- The requested resource (/webtest/servlet/hello.html) is not availabl 帮忙解决解决方法
- JSP 页面乱码 页面起首已设置 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
- 哪位高手能 帮小弟我在小弟我的电脑下 一个 做 freemarker 案例
- html js不运行有关问题
- 办公自动化系统——议程管理(用jsp+servlet+js+html+jdbc)怎样实现
- <html:text>property的有关问题
- 关于<html:text>相关的有关问题
- Struts2 标签 跟 freemarker
- Struts 1.2 html:form的action和form的action区别,还有如果2个都有执行哪个解决方案
- 简单计算器(html+js),该怎么解决
- HTML 小疑点,哪位高手解决 给哪位高手分
- 嵌入JSP中的CSS显示效果和在 HTML 中的不一样.为什么.该怎么解决
- struts1 中<html:checkbox>标签的value值怎么动态赋值
- 新人求问,J2EE方向,html,css,javascript,vml要学到什么程度?解决思路
- :前台和后台开发有啥区别?还有css、html、ajax、js、jquery都有什么区别
- freemarker 变量施用
- struts <html:file> 怎么让前面的框消失,先谢过了
- freemarker
- =Html.TextAreaFor的文本区域大小如何设置
- 正则表达式 提取 html 标签的内容,该如何解决
- @Html.CkEditor,该怎么处理
- string msg = (string)html.ViewData["Message"];该如何处理
- html password editbox 编码有关问题
- @Html.TextBoxFor(a => a.Title 上边有红线,说异常
- 关于伪静态,怎么配置.html
- MVC中,Html.DropDownListFor怎么绑定数据