freemarker 自定义标签
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 自定义标签的更多相关文章
- freemarker自定义标签报错(六)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered "\"\u4f60\u597d\uff01\& ...
- freemarker自定义标签报错(五)
freemarker自定义标签 1.错误描述 六月 05, 2014 11:40:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...
- freemarker自定义标签报错(四)
freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...
- freemarker自定义标签(一)
freemarker自定义标签 1.自定义标签说明 宏变量存储模板片段可以被用作自定义指令macro 2.示例说明 <html> <head> <meta http-eq ...
- freemarker自定义标签报错(三)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column ...
- freemarker自定义标签报错(二)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Unexpected end of file reached. at freemarker ...
- freemarker自定义标签报错(一)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgr ...
- freemarker自定义标签报错(七)
1.错误描述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...
- freemarker自定义标签(三)-nested指令
freemarker自定义标签 1.nested指令 是可选的,可以在<#macro>和</#macro>之间使用在任何位置和任意次数 2.示例说明 <#macro ta ...
- freemarker自定义标签(二)
freemarker自定义标签 1.自定义标签 通过自定义标签,写一个重复指定字符串 2.实现源码 <html> <head> <meta http-equiv=&quo ...
随机推荐
- mysql优化连接数防止访问量过高的方法
这篇文章主要介绍了mysql优化连接数防止访问量过高的方法,需要的朋友可以参考下 很多开发人员都会遇见”MySQL: ERROR 1040: Too many connections”的异常情况,造成 ...
- uva494 Kindergarten Counting Game
#include<bits/stdc++.h>using namespace std;int main(){ int n=0; char a; int flag=1; while((sca ...
- 直接操作游戏对象C#游戏开发
直接操作游戏对象C#游戏开发 2.2.3 直接操作游戏对象 在Inspector视图里通过设置属性而改变游戏场景中游戏对象的状态,太过抽象,毕竟数字并不够直观.其实,改变游戏对象的状态,完全有最最直 ...
- Catching Fish[HDU1077]
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Session赋值(备注)
Session赋值也是在后台赋,不是在前台赋 追问 不好意思 那还真能在AJAX中赋值 我已经解决了 加一个接口IRequiresSessionState 就OK 提问者评价 太感谢了,真心有用
- Ubuntu 12.04安装Google Chrome
详细请参照:http://hi.baidu.com/kevin276/item/29bc1c96a208fabc82d29542 至于安装之后怎么打开,在终端输入google-chrome即可打开并会 ...
- Java递归搜索指定文件夹下的匹配文件
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Queue; /** ...
- SQLLite 可以通过SQL语言来访问的文件型SQL数据库
Web Storage分为两类: - sessionStorage:数据保存在session 对象中(临时) - localStorage:数据保存在本地硬件设备中(永久) sessionStorag ...
- NBUT 1602 Mod Three(线段树单点更新区间查询)
[1602] Mod Three 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Please help me to solve this problem, if so, Liang ...
- Git Shell使用笔记
1,首次打开Git shell错误(以前打开过gethub客户端) 警告: git command could not be found. Please create an alias or add ...