谁有好使的jsp生成静态页面的源码呀,跪求了!!网上搜的都不好使,一定加分的

解决方案 »

  1.   

    package com.r.util;import java.io.*;
    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;
    }
    }
    }
      

  2.   

    参见:http://www.jdkcn.com/article.asp?id=25
    我感觉这个方法还是不错的,速度很快,也容易控制。
    上面是我封装的一个类,你参考一下吧。
      

  3.   

    freeer组建还没有试过呢,我在试试,完了散分,还有没有更好的呀,打算写成servlet的!!
      

  4.   

    官方网站上不去谁能给个freeer的jar包呀!!!
      

  5.   

    官方网站上不去谁能给个freeer的jar包呀!!!
    邮箱:[email protected]