主页:http://www.freemarker.org/index.html
获得数据:
package com.ninemax.nacao.business.template;
import com.ninemax.nacao.business.dataSearchInfo.clsResourceTypeBus;
import com.ninemax.nacao.business.searchEngine.SearchEngineBus;
import com.ninemax.nacao.to.dataSearchInfo.clsResourceTypeTO;
import com.ninemax.nacao.util.PageInfo;
import freemarker.core.Environment;
import freemarker.template.*;
import java.io.IOException;
import java.util.*;
public class ListNewReports1Info implements TemplateDirectiveModel {
private static final String PARAM_NAME_COUNT = "count";
private static final String PARAM_NAME_NAME = "name";
private static final String FROM = "from";
private static final String TO = "to";
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
int countParam = 0;
String unit_name = "";
int from = 0;
int to = 0;
Iterator paramIter = params.entrySet().iterator();
while (paramIter.hasNext()) {
Map.Entry ent = (Map.Entry) paramIter.next();
String paramName = (String) ent.getKey();
TemplateModel paramValue = (TemplateModel) ent.getValue();
if (paramName.equals(PARAM_NAME_COUNT)) {
countParam = ((TemplateNumberModel) paramValue).getAsNumber()
.intValue();
}else if (paramName.equals(FROM)) {
from = ((TemplateNumberModel) paramValue).getAsNumber()
.intValue();
}else if (paramName.equals(TO)) {
to = ((TemplateNumberModel) paramValue).getAsNumber()
.intValue();
} else if (paramName.equals(PARAM_NAME_NAME)) {
unit_name = paramValue.toString();
} else {
throw new TemplateModelException("Unsupported parameter: " + paramName);
}
}
HashMap<String, String> map = new HashMap<String, String>();
List<HashMap<String,String>> resultList1 =//业务部分
if (body != null) {
for (int i = from; i < to+1; i++) {
// 设置循环变量
if (loopVars.length > 0) {
try {
HashMap<String,String> maps = resultList1.get(i-1);
//业务部分
map.put("title", title);
map.put("id", maps.get("ID"));
map.put("content", maps.get("CONTENT"));
map.put("web", maps.get("WEBNAME"));
map.put("times", times);
//返回的两个参数
loopVars[0] = new SimpleHash(map);
loopVars[1] = new SimpleNumber(i);
} catch (Exception e) {
e.printStackTrace();
}
}
body.render(env.getOut());
}
}
}
}
放入ACTION中:
root.put("video",listVideosInfo);
从模版文件写到目标文件中:
Configuration cfg = null;
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(obj, null);
cfg.setTemplateUpdateDelay(30);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
cfg.setSharedVariable("trancDate",new TrancDateFunction());//自定义日期转换函数
cfg.setSharedVariable("subStr",new SubStringFunction());//自定义字符串截取函数
cfg.setSharedVariable("userDefineStr",new UserDefineStringFunction());//自定义字符串,两串相加
// /cfg.setSharedVariable("webSiteId",new WebSiteIdFunction());//自定义字符串截取函数
Template t = cfg.getTemplate(templatePath);
//t.setEncoding("gb2312");
t.setEncoding("gbk");
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(distFile)));
try {
t.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.close();
模版中的例子:
<@video count=3 from=1 to=3 name=' ';videos,num>
<#if videos.title != ""> //if判断
<DIV class="item">
<h1><img src="${videos.picture}" width="150" height="110" ></h1>
<h2><h3><a href="${videos.href}">${videos.title}</a></h3>
<h5><a href="${videos.href}">${videos.abs}</a></h5>
<h6><a href="">${videos.webname}</a></h6>
<h4>播放时间 ${videos.times} 上传时间 ${videos.uptime}</h4>
</h2>
</div>
</#if>
</@video>