freemarker


public class FreeMarkerFactory {

static Configuration cfg = 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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值