java freemarker 生成html,Java模板引擎技术freemarker生成静态页面实现网页静态化

package com.myfreemark.website;

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.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;

public class CreateHtmlWebSite {

public static final String FTLS_PATH ="ftls";

public static final String WEB_SITE_FTL ="MyWebSite.ftl";

public static final String CONST_HTML="/WebRoot/MyWebSite.html";

//设置加载模板

private static Template extracted(Configuration cfg) throws IOException {

cfg.setDirectoryForTemplateLoading(new File(FTLS_PATH)); //设置读取模板文件的目录

Template t = cfg.getTemplate(WEB_SITE_FTL); //读取文件名为Test.ftl的模板

return t;

}

public static void main(String[] args) throws TemplateException, IOException {

Configuration cfg = new Configuration();

Template t = extracted(cfg);

Map root = extracted();

extracted(t, root);

}

//设置生成路径

private static void extracted(Template t, Map root)

throws UnsupportedEncodingException, FileNotFoundException,

TemplateException, IOException {

String beanPath = System.getProperty("user.dir")+CONST_HTML;

System.out.println(beanPath);

Writer out = new OutputStreamWriter(new FileOutputStream(

beanPath), "utf-8"); //输出流

t.process(root, out); //动态加载root中的数据到Test.html。数据在模板中定义好了。

System.out.println("Create successfully!");

}

//进行初始化数据

private static Map extracted() {

Map> root = new HashMap(); //存储数据

ShowWebSiteView view1[] = new ShowWebSiteView[]

{

new ShowWebSiteView(

"http://herryhaixiao.iteye.com/",

"zesededyousxxxxx",

"ssniha****1",

"33333087@qq.com",

"2016.08.24 注册",

"",

"",

"ITEye"),

new ShowWebSiteView(

"http://herryhaixiao.iteye.com/",

"zesede",

"555012****1",

"4449144087@qq.com",

"2016.08.24 注册",

"",

"",

"ITEye")};

List list = new ArrayList();

for (ShowWebSiteView showWebSiteView : view1)

{

list.add(showWebSiteView);

}

root.put("weblist", list);

return root;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值