用freemarker生成静态页面

 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写
    FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用程序
虽然FreeMarker具有一些编程的能力,但通常由Java程序准备要显示的数据,由FreeMarker生成页面,通过模板显示准备的数据.
生成BT下载网站:http://www.bt285.cn,同样的:我也可以生成 http://www.tudousee.cn 
java 代码:

/**
*生成BT下载网站:http://www.bt285.cn,同样的:我也可以生成 http://www.tudousee.cn 
*/
public void createFtl(Map<String, Object> req){
        try {
            Configuration cfg = new Configuration();

            // - Templates are stoted in the WEB-INF/templates directory of the
            // Web app.
            String tempate = FileConstent.PrePath + "/tempates/";
            String destPath = tempate +"index/www.bt285.cn_index.html";
            String destPathSpider = tempate +"index/www.bt285.cn_index_spider.html";
           // new File(destPath).deleteOnExit();
           // new File(destPathSpider).deleteOnExit();
            cfg.setDirectoryForTemplateLoading(new File(tempate));
            Template tIndex = getTemplate(cfg,
                    "index/http://www.bt285.cn_index.ftl",Locale.ENGLISH,Constent.DefaultEncodIng);
            Template tIndexSpider = getTemplate(cfg,
                    "index/http://www.bt285.cn_index_spider.ftl",Locale.ENGLISH,Constent.DefaultEncodIng);        
            doWirte(tIndex,destPath,req);
            doWirte(tIndexSpider,destPathSpider,req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void doWirte(Template t,String path,Map<String, Object> req){
        try {
             //BufferedOutputStream bw = null;
            // FileOutputStream outStream = new FileOutputStream(path);
             //BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream,Constent.DefaultEncodIng));
             BufferedWriter out = new BufferedWriter(new FileWriter(path));
            // Merge the data-model and the template
            t.setEncoding(Constent.DefaultEncodIng);
            t.process(req,out);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    private WNewsDAO<WNews> newsDAO;

    private WCommentsDAO commentsDAO;
    
    public static Template getTemplate(Configuration cfg,String name,Locale locale,String decode)
            throws IOException{
        return cfg.getTemplate(name);
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值