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. tar.xz文件如何解压 (已验证)

    XZ压缩最新压缩率之王 xz这个压缩可能很多都很陌生,不过您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直很少,所以几乎没有什么提起. 我是在下载phpmyadmin的时候看到 ...

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

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

  3. Xamarin Android开发实战(上册)大学霸内部资料

    Xamarin Android开发实战(上册)大学霸内部资料   试读文档下载地址:http://pan.baidu.com/s/1jGEHhhO 密码:vcfm 介绍: 本教程是国内唯一的Xamar ...

  4. Beaglebone Black的启动

    Beaglebone Black的启动 第1章  准备开始 Beaglebone Black上最显眼的恐怕就是板子两侧的扩展端口,一侧有46个端口,共92个端口Beaglebone Black的启动. ...

  5. 加密 bouncy castle

    1.去官方站点下载Bouncy Castle的JCE Provider包 bcprov-ext-jdk15-145.jar 2.把jar文件复制到 $JAVA_HOME$\jre\lib\ext 目录 ...

  6. js对象数组按属性快速排序

    前一篇<关于selector性能比赛>中提到,目测觉得在$("div,p,a")这样有逗号时,sizzle耗时异常(600多个元素,花了200ms),说是它可能没有优化 ...

  7. UVa 11181 条件概率

    题意:n个人选r个人,每个人被选中的概率为pi,问最后每个人被选中的概率是多少. sol:就是个简单的概率题,范围还特别小,深搜秒出...然而公式什么的很多还是需要注意的...     条件概率的公式 ...

  8. codevs 1507酒厂选址

    #include<cstdio> #include<cstdlib> using namespace std; int n; int dis[10010],a[10010],x ...

  9. 获取枚举类型Description特性的描述信息

    C#中可以对枚举类型用Description特性描述. 如果需要对Description信息获取,那么可以定义一个扩展方法来实现.代码如下: public static class EnumExten ...

  10. c# 使用GetOleDbSchemaTable获取access数据库结构

    c# 使用GetOleDbSchemaTable获取access数据库结构 ado.net可以使用GetOleDbSchemaTable方法来获取access数据库的结构,但得到的datatable的 ...