freemarker
public class FreeMarkerFactory {static Configurationcfg = new Configuration();/* 处理一个list模板: * <#list news as new>title:${new.title}content:${new.content}date:${new.date}</#list> */public static void createHtml() throws IOException, TemplateException{cfg.setDirectoryForTemplateLoading(new File("D:\\eclipse3.5workspace\\freemarker\\html\\"));List<New> news= new ArrayList<New>();New newz = new New("","","");newz.setPath("D:\\eclipse3.5workspace\\freemarker\\html\\1.html");newz.setTitle("111111111111");news.add(newz);New newz2 = new New("","","");newz2.setPath("D:\\eclipse3.5workspace\\freemarker\\html\\2.html");newz2.setTitle("22222222222222");news.add(newz2);Map map = new HashMap();map.put("news", news);Template t = cfg.getTemplate("news.ftl");File htmlFile = new File("D:\\eclipse3.5workspace\\freemarker\\html\\"+"news.html"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));t.process(map, out);}/*处理一个对象 模板: * title:${title}content:${content}date:${date} */public static void createHtml2() throws IOException, TemplateException{cfg.setDirectoryForTemplateLoading(new File("D:\\eclipse3.5workspace\\freemarker\\html\\"));New news = new New("第一个新闻","第一个新闻内容",new Date().toGMTString());Map<String, New> newMap = new HashMap<String, New>();newMap.put("news",news);Template t = cfg.getTemplate("news2.ftl");t.process(news, new OutputStreamWriter(System.out));}/*处理一个对象 生成文件 *title:${title}content:${content}date:${date} */public static void createHtml3() throws IOException, TemplateException{cfg.setDirectoryForTemplateLoading(new File("D:\\eclipse3.5workspace\\freemarker\\html\\"));New news = new New("第2个新闻","第2个新闻内容",new Date().toGMTString());Map<String, New> newMap = new HashMap<String, New>();newMap.put("news",news);Template t = cfg.getTemplate("news2.ftl");File htmlFile = new File("D:\\eclipse3.5workspace\\freemarker\\html\\"+"2.html"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));t.process(news, out);out.flush();out.close();}public static void main(String[] args) throws IOException, TemplateException {FreeMarkerFactory.createHtml();//FreeMarkerFactory.createHtml3();}}====================================
//if用法
<#if links?size != 0><div class="link"> <strong>友情链接:</strong> <#list links as link> <a href="${link.linkUrl}" target="_blank" title="${link.linkName}">${link.linkName}</a> </#list></div><#else><div class="link"></div></#if>
//多list显示//ftl:<html><head><title></title></head><body><#if articles?size !=0><div id="div1"><#list articles as article>${article.title}<br></#list></div><#else></#if><h1><p>第二个list显示</p></h1><div>你们好,good freemarker</div><#if articles1?size !=0><div id = "div2"><#list articles1 as article1>${article1.title}<br></#list></div><#else></#if></body></html>============================public void createLists() throws IOException, TemplateException{cfg.setDirectoryForTemplateLoading(new File("D:/eclipseWorkspace/baby/test/com/baby/freemark/")); List<Article> articles = new ArrayList<Article>();for (int i = 0; i < 5; i++) {Article article = new Article();article.setTitle("article 1 list title 第 " + i);articles.add(article);}List<Article> articles1 = new ArrayList<Article>();for (int i = 0; i < 10; i++) {Article article = new Article();article.setTitle("article 2 list title 第 " + i);articles1.add(article);}Map map = new HashMap();map.put("articles", articles);map.put("articles1", articles1);cfg.setDirectoryForTemplateLoading(new File("D:/eclipseWorkspace/baby/test/com/baby/freemark/")); Template t = cfg.getTemplate("listTemplate.ftl"); File htmlFile = new File(path + "list.html"); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(htmlFile), "gbk")); t.process(map, out); out.flush(); out.close();}
页:
[1]