论坛首页 入门技术论坛

freemarker 入门

浏览 4969 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-01-23  
Header.ftl文件代码
<#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>


copyright.ftl文件代码
<table>jamesby copyright</table>
</body>
</html>


helloworld.ftl文件代码
<#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">


java文件代码
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................");
    }
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics