freemarker 模版靜態化

2021-07-22 11:59:44 字數 1904 閱讀 8960

思路很簡單,建立模板(.ftl) --->將模板和資料揉在一起----> 生成html檔案   

1.建立ftl模板

public void consultaiontohtml(string cid) else if(vo.getremarks().equals("1"))else if(vo.getremarks().equals("2"))

}if(null!=vo&&vo.getconsultaiontype().equals("cheats")&&vo.getcontentlink()==null)else if(null!=vo&&!vo.getconsultaiontype().equals("cheats"))else if(vo.getconsultaiontype().equals("info"))

listcitylist=gdmanagerservice.getgdcitylevel1();

for(gdsmcity city : citylist)

string imgurl="";

if(vo!=null&&vo.getlogourl()!=null)else

map map = new hashmap();

map.put("publicopenid",publicopenid);

map.put("consultaionid",vo.getid());

map.put("title",vo.gettitle());

map.put("time",staticmethod.formatdatetostring(vo.getcreatetime(), "yyyy-mm-dd"));

map.put("content",vo.getcontent().replaceall(resourcelocator.gedai_consultaion_pic_path,getsiteurl()+"/"+resourcelocator.gedai_consultaion_pic_path));

map.put("cityhtml",cityhtml);

map.put("path",getsiteurl());

map.put("shareimageurl",imgurl);

map.put("sharedesc","宜信為沒有受到傳統金融體系重視的高成長性人群提供普惠金融服務宜信普惠。");

map.put("remarks",remarks);

map.put("functiontype",functiontype);

string path = resourcelocator.getresourceabsolutelocationpath(resourcelocator.gedai_consultaion_html_path);

for(gdsmcity city : citylist)

logger.info("資訊【id={}】靜態化生成成功。", cid);

}} catch (exception e) 】靜態化生成失敗。",cid);

logger.error(e.getmessage(), e);}}

public void ouputfile(string ftlname, string filename, mapmap,string path) throws exception

//定義輸出流,注意的必須指定編碼

writer writer = new bufferedwriter(new outputstreamwriter(new fileoutputstream(new file(path+"/"+filename)),"utf-8"));

//生成模板

template.process(map, writer);

writer.close();

}

2.生成html檔案

3.訪問生成的html檔案

freemarker 模版靜態化

public void consultaiontohtml string cid else if vo.getremarks equals 1 else if vo.getremarks equals 2 if null vo vo.getconsultaiontype equals cheats ...

Freemarker網頁靜態化

freemarker的jar包 org.freemarker freemaeker 2.3.23 autowired private freemarkerconfigurer freemarkerconfigurer responsebody public string genhtml throws...

網頁靜態化之Freemarker

為什麼要使用網頁靜態化技術呢?提供併發承載量對於一些瞬時訪問量巨大的 減輕資料庫壓力.它和快取技術的共同點都是為了減輕資料庫的訪問壓力但是運用的場景不同.快取比較適合小規模的資料,而網頁靜態化比較適合大規模且相對變化不太頻繁的資料,同時它有利於seo 運用 比如在電商中商品詳情頁或者在,的文章報道。...