请教freemark生成静态html页面的问题

nickowen 2007-11-09 12:26:03
请教各位,能不能贴一份具体能用的代码给我。我在网上找了几份都不管用。
/**
* 获取freemarker的配置. freemarker本身支持classpath,目录和从ServletContext获取.
*/
protected Configuration getFreeMarkerCFG()
{
if (null == freemarker_cfg)
{
// Initialize the FreeMarker configuration;
// - Create a configuration instance
freemarker_cfg = new Configuration();

// - FreeMarker支持多种模板装载方式,可以查看API文档,都很简单:路径,根据Servlet上下文,classpath等等

//htmlskin是放在classpath下的一个目录
freemarker_cfg.setClassForTemplateLoading(this.getClass(), "/htmlskin");
}

return freemarker_cfg;
}
附上其中的一个方法,我的问题是。这个方法中的
freemarker_cfg.setClassForTemplateLoading(this.getClass(), "/htmlskin");
总提示我找不到 模板
12:16:58,593 ERROR Test:112 - Error while generate Static Html File 1.htm
java.io.FileNotFoundException: Template /aa/test.ftl not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:441)
at freemarker.template.Configuration.getTemplate(Configuration.java:404)
at com.t2cn.news.init.Test.geneHtmlFile(Test.java:94)
at com.t2cn.news.init.Test.main(Test.java:54)

方法来源于 http://www.jscud.com/news/viewnews.jspa?nid=36 ——使用FreeMarker生成Html静态文件(实例) (05-04-07)

麻烦各位帮个忙了,小弟感激不尽!!
...全文
789 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
joy911 2010-12-15
  • 打赏
  • 举报
回复
期待解决 ftl文件中引入 struts2标签(<#assign s=JspTaglibs["/WEB-INF/tld/struts-tags.tld"]>) 后,如何转成html????
joy911 2010-12-15
  • 打赏
  • 举报
回复
如果ftl文件中引入 <#assign s=JspTaglibs["/WEB-INF/tld/struts-tags.tld"]>
还能转成html吗???
lingyun5905 2010-07-17
  • 打赏
  • 举报
回复
danyajuan 2008-12-17
  • 打赏
  • 举报
回复
我也是在这个地方卡壳,先谢过
wang76_cn 2008-10-23
  • 打赏
  • 举报
回复
这篇文章真是太好了.

解决了我的一个大难题

谢谢!
igyhi 2007-12-04
  • 打赏
  • 举报
回复
package Util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* 静态页面生成工具类
* @author igyhi
* @version 2007.12.04
*/
public class BuildHtml
{
/**
* 创建一个静态页面
* @param tpl 加载有ftl文件的模板类
* @param ftldataMap 模板文件中的数据集合
* @param htmlfile 含路径的静态文件名
*/
public boolean create(Template tpl,Map ftldataMap,String htmlfile)
{
File htmlFile = new File(htmlfile);
Writer out;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),"UTF-8"));
tpl.process(ftldataMap,out);
out.flush();
return true;
}catch (Exception e)
{e.printStackTrace();return false;}
}
/**
* 创建一个静态页面
* @param ftlpath 模板文件ftl的路径
* @param ftlname 模板文件名
* @param ftldataMap 模板文件中的数据集合
* @param htmlfile 含路径的静态文件名
*/
public boolean create(String ftlpath,String ftlname,Map ftldataMap,String htmlfile)
{
Configuration con=new Configuration();
Template t=null;
try {
con.setDirectoryForTemplateLoading(new File(ftlpath));
t = con.getTemplate(ftlname,"UTF8");
} catch (IOException e1)
{e1.printStackTrace();}

return create(t,ftldataMap,htmlfile);
}

/*
public static void main(String args[])
{
BuildHtml bh=new BuildHtml();
Map r=new HashMap();
Map des=new HashMap();
des.put("name","坦克");
des.put("age","231");
r.put("root",des);
bh.create("C:/Tomcat5.5/webapps/freemarker/WEB-INF/templates","bh.ftl",r,"C:/Tomcat5.5/webapps/freemarker/aaa/bbb/bhbh.html");
}
*/
}

81,116

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧