---
title: "freemark"
author: "xihuan&java"
date: "2013-04-01T13:58:58Z"
tags: []
categories: ["Web前端"]
slug: "freemark"
draft: false
lastmod: "2013-04-01T13:58:58Z"
id: 1840011
url: "https://wdwheyr.iteye.com/blog/1840011"
---

templtUtil： public static final String P\_TEMPLATE = "p.ftl"; public static final String TEMPLATE = "pr.ftl"; public static final String TEMPLATE\_PATH = "/vm"; public static final String PREVIEW\_HTML = "/pr.html"; public static Template configTemplate(DynamoHttpServletRequest request, String temp) throws IOException { Configuration config = new Configuration(); ServletContext sc = request.getSession().getServletContext(); config.setDirectoryForTemplateLoading(new File(sc.getRealPath(TEMPLATE\_PATH))); config.setObjectWrapper(new DefaultObjectWrapper()); Template template = config.getTemplate(temp, "UTF-8"); return template; } public static void toPreview(DynamoHttpServletRequest request, String temp, Map root){ try { String previewPath = request.getSession().getServletContext().getRealPath("")+PREVIEW\_HTML; Template template = configTemplate(request, temp); FileOutputStream fos = new FileOutputStream(previewPath); Writer out = new OutputStreamWriter(fos, "UTF-8"); template.process(root, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } servlet代码： public boolean promotionPicPreview(DynamoHttpServletRequest request,DynamoHttpServletResponse response) { if (promotionPicRequest(request, response)) { try { response.setContentType("text/plain;charset=UTF-8"); response.setHeader("Cache-Control","no-cache"); response.sendRedirect(this.PREVIEW); } catch (IOException e) { e.printStackTrace(); } } return false; } public boolean promotionPicSave(DynamoHttpServletRequest request,DynamoHttpServletResponse response) { try { if (promotionPicRequest(request, response)) { File previewFile = new File(request.getSession().getServletContext().getRealPath(CreateHtmlUtil.PREVIEW\_HTML)); InputStream is = new FileInputStream(previewFile); response.reset(); response.setContentType("application/x-msdownload;charset=UTF-8"); response.addHeader("Content-Disposition","attachment; filename=\\"" + PROMOTION\_HTML + "\\""); byte\[] b = new byte\[1024]; int len; while ((len=is.read(b)) &gt;0) { response.getOutputStream().write(b,0,len); } is.close(); response.getOutputStream().flush(); response.getOutputStream().close(); } } catch (Exception e) { e.printStackTrace(); return false; } return false; } public boolean promotionPicRequest(DynamoHttpServletRequest request,DynamoHttpServletResponse response){ String topBanner = request.getParameter(TOP\_BANNER); String backColor = request.getParameter(BACK\_COLOR); String moreActivity = request.getParameter(MORE\_ACTIVITY); String\[] banner = request.getParameterValues(BANNER); String\[] pic1 = request.getParameterValues(PIC\_1); String\[] picLink1 = request.getParameterValues(PIC\_LINK\_1); String\[] pic2 = request.getParameterValues(PIC\_2); String\[] picLink2 = request.getParameterValues(PIC\_LINK\_2); String\[] pic3 = request.getParameterValues(PIC\_3); String\[] picLink3 = request.getParameterValues(PIC\_LINK\_3); String\[] pic4 = request.getParameterValues(PIC\_4); String\[] picLink4 = request.getParameterValues(PIC\_LINK\_4); try { Map root = new HashMap(); List&gt; lines = new ArrayList&gt;(); for (int i=0; i &lt; pic1.length; i++) { String ban = banner\[i]; Map line = new HashMap(); List picsList = new ArrayList(); Map map = new HashMap(); map.put("pic1", pic1\[i]); map.put("pic2", pic2\[i]); map.put("pic3", pic3\[i]); map.put("pic4", pic4\[i]); map.put("picLink1",picLink1\[i]); map.put("picLink2", picLink2\[i]); map.put("picLink3", picLink3\[i]); map.put("picLink4", picLink4\[i]); picsList.add(map); line.put("pics", picsList); line.put("banner", ban); lines.add(line); } root.put("top\_banner", topBanner); root.put("back\_color", backColor); root.put("more\_activity", moreActivity); root.put("lines", lines); CreateHtmlUtil.toPreview(request, CreateHtmlUtil.PIC\_TEMPLATE, root); return true; } catch (Exception e) { e.printStackTrace(); return false; } } 模板代码： &lt;#list lines as line&gt; &lt;#if line.xxx != "" &gt; &lt;#list line.ps as p&gt; ${p.xxx}