您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)web開發(fā)中頁面靜態(tài)化的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
public void GenerathHtmlByString() throws IOException, TemplateException {
//創(chuàng)建配置類
Configuration configuration=new Configuration(Configuration.getVersion());
//模版內(nèi)容
String templateString="<html>\n" +
"<head>\n" +
" <meta charset="UTF-8">\n" +
" <title>FreemarkerTest1</title>\n" +
"</head>\n" +
"<body>\n" +
" 老同志,你也不要拽,${song}!\n" +
" \n" +
"</body>\n" +
"</html>";
//模版加載器
StringTemplateLoader loader=new StringTemplateLoader();
loader.putTemplate("template", templateString);
configuration.setTemplateLoader(loader);
//得到模版
Template template=configuration.getTemplate("template", "UTF-8");
?
//定義數(shù)據(jù)模型
Map<String,Object> map=new HashMap<>();
map.put("song", "你一個月的工資才二十來塊");
//靜態(tài)化
String content= FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println("我們打印一下靜態(tài)化的內(nèi)容 :" + content);
?
//轉(zhuǎn)化為流
InputStream inputStream= IOUtils.toInputStream(content);
//輸出文件
FileOutputStream fileOutputStream=new FileOutputStream(new File("F:\\test2.html"));
int copy= IOUtils.copy(inputStream, fileOutputStream);
System.out.println(copy);//209
}
感謝各位的閱讀!關(guān)于“web開發(fā)中頁面靜態(tài)化的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。