谁有好使的jsp生成静态页面的源码呀,跪求了!!网上搜的都不好使,一定加分的
解决方案 »
- JSP和FLEX实现在线文档播放器的问题
- 输入框对齐的问题
- jdbc执行sql的奇怪问题,请大家解惑
- 如何鼠标放到<img>改变图片
- 用jxl转Excel怎样画斜线
- javascript怎么实现只打印<input type="text">的内容?
- xml字符串怎样转换成xml文档,或者是xml字符串怎么能被正确的解析(中文不能解析)
- hibernate+mysql 插入时报错 Column XXX specified twice
- 大家有兴趣来看看。 客户端内容相互篡位! (来者必给分。)
- 问题是tomcat+sqlserver2000怎么配置,需要怎么设置,谢谢先!
- jsp session失效了?请高手指教:
- myeclipse如何打包成jar?
import java.util.Map;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import freeer.template.*;public class MakeHtmlFile { private final Log logger = LogFactory.getLog(getClass()); private Configuration freeerCfg = null; private String templatePath = ""; private String realBuildPath = ""; /**
*
* @param realBuildPath根目录绝对路径
* @param templatePath模板相对路径,不包含模板名称
*/
public MakeHtmlFile(String realBuildPath, String templatePath) {
this.templatePath = templatePath;
this.realBuildPath = realBuildPath;
setTemplatePath();
} /**
*
* @param templatePath:模板绝对路径
*/
private void setTemplatePath() {
// 设置freeer的参数
freeerCfg = new Configuration();
try {
File file = new File(realBuildPath+templatePath);
freeerCfg
.setDirectoryForTemplateLoading(file);
freeerCfg.setObjectWrapper(new DefaultObjectWrapper());
freeerCfg.setDefaultEncoding("UTF-8");
} catch (IOException ex) {
System.out.println("No Directory found,please check you config.\n"
+ realBuildPath+templatePath);
}
} /**
* 生成静态文件
*
* @param templateFileName
* 模版名称eg:(biz/order.ftl)
* @param propMap
* 用于处理模板的属性Object映射
* @param htmlFilePath
* 要生成的静态文件的路径,相对设置中的根路径,例如 "/biz/2006/5/"
* @param htmlFileName
* 要生成的文件名,例如 "123.htm"
* @return
*/
public boolean buildHtml(String templateFileName, Map propMap,
String htmlFilePath, String htmlFileName) {
try {
Template template = freeerCfg.getTemplate(templateFileName);
template.setEncoding("UTF-8");
// 创建生成文件目录
creatDirs(realBuildPath, htmlFilePath);
File htmlFile = new File(realBuildPath + htmlFilePath
+ htmlFileName);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "UTF-8"));
template.process(propMap, out);
out.flush();
return true;
} catch (TemplateException ex) {
ex.printStackTrace();
logger.error("Build Error" + templateFileName, ex);
return false;
} catch (IOException e) {
logger.error("Build Error" + templateFileName, e);
return false;
} } /**
* 创建多级目录
*
* @param aParentDir
* String
* @param aSubDir
* 以 / 开头
* @return boolean 是否成功
*/
public static boolean creatDirs(String aParentDir, String aSubDir) {
File aFile = new File(aParentDir);
if (aFile.exists()) {
File aSubFile = new File(aParentDir + aSubDir);
if (!aSubFile.exists()) {
return aSubFile.mkdirs();
} else {
return true;
}
} else {
return false;
}
}
}
我感觉这个方法还是不错的,速度很快,也容易控制。
上面是我封装的一个类,你参考一下吧。
邮箱:[email protected]