FreeMarker 经典入门例子
废话不多说直接上源码
导入freemarker-2.3.8.jar包
1.FreeMarker类
package com.uitl;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.io.StringWriter;import java.util.HashMap;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.DefaultObjectWrapper;import freemarker.template.Template;import freemarker.template.TemplateException;/*** * * @author ZhuangZi * @version $Id: Test.java,v 0.1 2013-3-5 下午02:44:35 ZhuangZi Exp $ */public class FreeMarker { @SuppressWarnings("unchecked") /*** * @author Administrator * @Directions 生成HTML模板 * @param ftlpath 模板存放路径 * @param htmlpath HTML文件存放路径 * @param tempName 生成HTML文件名 * @return null */ public static void creatHtml(String ftlpath, String htmlpath, String tempName) { /* 在整个应用的生命周期中,这个工作你应该只做一次。 */ /* 创建和调整配置。 */ Configuration cfg = new Configuration(); try { cfg.setDirectoryForTemplateLoading(new File(ftlpath)); cfg.setObjectWrapper(new DefaultObjectWrapper()); /* 在整个应用的生命周期中,这个工作你可以执行多次 */ /* 获取或创建模板*/ Template temp = cfg.getTemplate(tempName + ".ftl"); /* 创建数据模型 */ Map root = new HashMap(); root.put("user", "Big Joe"); Map latest = new HashMap(); root.put("latestProduct", latest); latest.put("url", "products/greenmouse.html"); latest.put("name", "green mouse"); /* 将模板和数据模型合并 */ StringWriter out = new StringWriter(); temp.process(root, out); System.out.println(out.toString()); //写入html文件 File file = new File(htmlpath + tempName + ".html"); if (!file.exists()) { file.mkdirs(); } if (file.exists()) { file.delete(); } RandomAccessFile ra = null; ra = new RandomAccessFile(file, "rw"); ra.write(out.toString().getBytes("gb2312")); ra.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } } public static void main(String[] args) { creatHtml("WebRoot/ftl", "WebRoot/html", "test"); }}
2.WebRoot/ftl文件夹下的模板文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>test.ftl</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h1>Welcome ${user}<#if user == "Big Joe">, our belovedleader</#if>!</h1><p>Our latest product:<a href="${latestProduct.url}">${latestProduct.name}</a>! </body></html>3.运行FreeMarker类的main方法,会自动在.WebRoot下创建html文件
OK了,你就使劲顶贴吧 哈哈