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; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值