1.编写标签类

package com.pccw.business.fnd.common.filegen;

import java.io.IOException;
import java.io.Writer;
import java.util.Map; import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException; public class FinalReport2Directive implements TemplateDirectiveModel { /**
* @param env
* 上下文变量
* @param params
* 标签参数
* @param loopVars
* @param body
* 标签体
*/
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
TemplateModel prjojectNumTM = (TemplateModel)params.get("projectNum");
System.out.println(prjojectNumTM.toString()); body.render(new FinalReportCustomWriter(env.getOut())); } private static class FinalReportCustomWriter extends Writer { private final Writer out; FinalReportCustomWriter(Writer out) {
this.out = out;
} public void write(char[] cbuf, int off, int len) throws IOException { StringBuffer buf = new StringBuffer();
buf.append("<tr><td>11</td></tr>");
for (int i = 0; i < 50; i++) {
buf = new StringBuffer();
buf.append("<tr><td>" + i + "orderName" + "</td></tr>");
buf.append("<tr><td>" + i + "orderNum" + "</td></tr>");
buf.append("<tr><td>" + i + "orderQuantity" + "</td></tr>");
buf.append("<tr><td>" + i + "orderPrice" + "</td></tr>");
out.write(buf.toString());
}
} public void flush() throws IOException {
out.flush();
} public void close() throws IOException {
out.close();
}
}
}

2.  编写文件生成类,单例

package com.pccw.business.fnd.common.filegen;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map; import javax.servlet.ServletContext; import freemarker.template.Configuration;
import freemarker.template.Template; public class HtmlFileBuild { private static HtmlFileBuild htmlFileBuild = null;
private Configuration configuration; private HtmlFileBuild(){
configuration = new Configuration();
} public static HtmlFileBuild getInsance(){
if(htmlFileBuild == null){
htmlFileBuild = new HtmlFileBuild();
}
return htmlFileBuild;
} /**
*
* @param context
* 上下文
* @param data
* 绑定数据
* @param templateFileName
* 模板名称
* @param targetHtmlFileName
* 生成目标文件名称
* @return 生成html文件路径
* @throws Exception
*/
public String crateHTML(ServletContext context, Map<String, Object> data, String templateFileName,
String targetHtmlFileName) throws Exception { try {
// 模板存放路径
this.configuration.setDirectoryForTemplateLoading(new File(
"D:/projects/FAS/trunk/dev/arch/WebRoot/business/template")); // 模板文件名称
Template template = this.configuration.getTemplate(templateFileName);
template.setEncoding("UTF-8");
// 静态页面要存放的路径
String htmlPath = targetHtmlFileName;
File htmlFile = new File(htmlPath);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
// 处理模版 map数据 ,输出流
data.put("projectNum", "B00002");
template.process(data, out);
out.flush();
out.close();
return targetHtmlFileName;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} // test
public static void main(String arg[]) throws Exception {
new HtmlFileBuild().crateHTML(null, new HashMap(), "finalReport2.ftl", "E:/tmp/freemarker/finalReport2.html");
}
}

3. finalReport2.ftl

<#assign fr2 = "com.pccw.business.fnd.common.filegen.FinalReport2Directive"?new()>
<table style="border:1px">
<@fr2 projectNum="${projectNum}"> </@fr2>
</table>

freemarker 自定义标签的更多相关文章

  1. freemarker自定义标签报错(六)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered "\"\u4f60\u597d\uff01\& ...

  2. freemarker自定义标签报错(五)

    freemarker自定义标签 1.错误描述 六月 05, 2014 11:40:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...

  3. freemarker自定义标签报错(四)

    freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...

  4. freemarker自定义标签(一)

    freemarker自定义标签 1.自定义标签说明 宏变量存储模板片段可以被用作自定义指令macro 2.示例说明 <html> <head> <meta http-eq ...

  5. freemarker自定义标签报错(三)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column ...

  6. freemarker自定义标签报错(二)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Unexpected end of file reached. at freemarker ...

  7. freemarker自定义标签报错(一)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgr ...

  8. freemarker自定义标签报错(七)

    1.错误描述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...

  9. freemarker自定义标签(三)-nested指令

    freemarker自定义标签 1.nested指令 是可选的,可以在<#macro>和</#macro>之间使用在任何位置和任意次数 2.示例说明 <#macro ta ...

  10. freemarker自定义标签(二)

    freemarker自定义标签 1.自定义标签 通过自定义标签,写一个重复指定字符串 2.实现源码 <html> <head> <meta http-equiv=&quo ...

随机推荐

  1. 修改ViewPager调用setCurrentItem时,滑屏的速度

    原文摘自: 修改ViewPager调用setCurrentItem时,滑屏的速度 在使用ViewPager的过程中,有需要直接跳转到某一个页面的情况,这个时候就需要用到ViewPager的setCur ...

  2. 【HTML5】Application Cache应用程序缓存

    HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载 ...

  3. Xamarin.iOS编译时无法连接苹果系统

    Xamarin.iOS编译时无法连接苹果系统   错误信息:Unable to connect to Address=’***.***.***.***’ with User=’***’   即使Vis ...

  4. Xamarin Android提示内存溢出错误

    Xamarin Android提示内存溢出错误 错误信息:java.lang.OutOfMemoryError, Consider increasing the value of $(JavaMaxi ...

  5. HealthKit开发快速入门教程之HealthKit开发概述简介

    HealthKit开发快速入门教程之HealthKit开发概述简介 2014年6月2日召开的年度开发者大会上,苹果发布了一款新的移动应用平台,可以收集和分析用户的健康数据.该移动应用平台被命名为“He ...

  6. ZOJ1655 Transport Goods(Floyd)

    利用Floyd的DP状态转移方程. #include<cstdio> #include<cstring> #include<queue> #include<a ...

  7. Revit二次开发示例:Journaling

    关于Revit Journal读写的例子.   #region Namespaces using System; using System.Collections.Generic; using Sys ...

  8. [Unity3D]脚本中Start()和Awake()的区别

    Unity3D初学者经常把Awake和Start混淆. 简单说明一下,Awake在MonoBehavior创建后就立刻调用,Start将在MonoBehavior创建后在该帧Update之前,在该Mo ...

  9. 2015ACM/ICPC亚洲区长春站 E hdu 5531 Rebuild

    Rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  10. 精通CSS :nth-child伪类

    :nth-child 基本用法 :nth-child:nth-child(8) 选中第8个子元素 li:nth-child(8) span { background-color: #298EB2; b ...