freemarker 入門

[b]Header.ftl文件代碼[/b]
[code]<#macro title color="red">
<font color="${color}" size="16px"><#nested></font>
</#macro>
<#assign version = "1.0">
<html>
<head>
<title>Hello World ${version}</title>
</head>
<body>[/code]

[b]copyright.ftl文件代碼[/b]
[code]<table>jamesby copyright</table>
</body>
</html>[/code]

[b]helloworld.ftl文件代碼[/b]
[code]<#import "header.ftl" as header>
<@header.title color="red">Hello World ${header.version}</@header.title><br>
<#if user.usertype = "buyer">${message} ${user.userName} Buyer!<#else>${message} ${user.userName} Seller!</#if>
<#if satuday??>No Trade<#else>Please trade.........</#if>
<#-- Goods List -->
<table border=1>
<tr>
<td>Goods Name</td>
<td>Price</td>
<td>updateDate</td>
</tr>
<#list goods as being>
<tr>
<td>${being.name?html}</td>
<td align="right">
<#if being.price gt 500>
<font color=red>${being.price!"NULL"}</font>
<#else>
<font color=blue>${being.price}</font>
</#if>
</td>
<td>${being.dt?string("yyyy-MM-dd HH:mm:ss")}
</td>
</#list>
</table>
<table>
<#list ["buy"]+["sell"] as x>
<a href="#">${x}</a>
</#list>
</table
<#include "copyright.ftl">[/code]

[b]java文件代碼[/b]
[code]package test.t0121;

import java.util.*;
import java.io.*;
import freemarker.template.*;

public class HelloWorld{
private Configuration cfg;

public Configuration getCfg() {
return cfg;
}
public void init() throws Exception{
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("D:/freemarker_project/template"));
}
public static void main(String[] args) throws Exception
{
User user = new User("james",User.USER_BUYER);
HelloWorld obj = new HelloWorld();
obj.init();
Map root = new HashMap();
root.put("message", "Hello ");
root.put("user",user);

List goods = new ArrayList();
root.put("goods",goods);

Goods goodsA = new Goods("GoodsA",10,new Date());
Goods goodsB = new Goods("GoodsB",100,new Date());
Goods goodsC = new Goods("GoodsC",1000,new Date());
goods.add(goodsA);
goods.add(goodsB);
goods.add(goodsC);

Template t = obj.getCfg().getTemplate("helloworld.ftl");

Writer out = new OutputStreamWriter(new FileOutputStream("D:/freemarker_project/out/helloword.html"),"GBK");
t.process(root, out);
System.out.println("Successfull................");
}
}[/code]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章