ITeye博客

freemark

xihuan&java · 2013-04-01 13:58 · freemark · Web前端 · Markdown

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)) >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> lines = new ArrayList>(); for (int i=0; i < 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; } } 模板代码: <#list lines as line> <#if line.xxx != "" > <#list line.ps as p> ${p.xxx}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。