Freemarker生成静态页面

今天要弄一个生成页面的,实际中,一般不用绝对路径的。

生成页面类:

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; @SuppressWarnings("unchecked") public class TemplateTest { /** * * 生成静态文件 * @param propMap * 模板的属性Object映射 * @param templatPath * 模板文件的路径 * @param tmplateName * 模板名称 * @param generateFilePath * 要生成的静态文件的路径 * @param generateFileName * 要生成的文件名 */ public static void generateTemplate(Map propMap,String templatePath,String templateName,String generateFilePath,String generateFileName){ Configuration cfg = new Configuration(); Template temp = null; Writer out = null; cfg.setDefaultEncoding("utf-8"); cfg.setNumberFormat("#"); cfg.setDateFormat("yyyy-MM-dd"); cfg.setDateTimeFormat("yyyy-MM-dd HH:mm:ss"); cfg.setTimeFormat("HH:mm:ss"); try { cfg.setDirectoryForTemplateLoading(new File(templatePath)); cfg.setObjectWrapper(new DefaultObjectWrapper()); temp = cfg.getTemplate(templateName); out = new OutputStreamWriter(new FileOutputStream(generateFilePath+generateFileName),"UTF-8"); temp.process(propMap, out); out.flush(); }catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(out!=null){ try { out.close(); out = null; } catch (IOException e) { e.printStackTrace(); } } } } }

源文件:testConfiguration.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>测试freemarker生成页面</title> </head> <body> card:${cardId?if_exists}<br> name:${userName?if_exists}<br> age :${age?if_exists}<br> gender: <#if gender="1">女<#else>男</#if> </body> </html>

测试类:

import java.util.HashMap; import java.util.Map; import java.util.Random; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class TestFreemarker extends ActionSupport{ public String testTemplate(){ Random rand = new Random(); Map<String,String> propMap = new HashMap<String, String>(); propMap.put("cardId", String.valueOf(rand.nextInt(10000000))); propMap.put("userName", "testName"+rand.nextInt(10)); propMap.put("age", String.valueOf(rand.nextInt(100))); propMap.put("gender", String.valueOf(rand.nextInt(1))); TemplateTest.generateTemplate(propMap, "D:/dev/Tomcat/webapps/testConfiguration/", "testConfiguration.ftl", "D:/dev/Tomcat/webapps/testConfiguration/", "testConfiguration.html"); addActionMessage("生成testConfiguration.html页面成功"); return SUCCESS; } }

转载请标明出处:龙企阁 http://blog.csdn.net/longxia1987

你可能感兴趣的:(freemarker,String,XHTML,null,Random)