SpringBoot如何用java生成靜態(tài)html
更新時間:2022年06月29日 14:09:19 作者:??七號im????
這篇文章主要介紹了SpringBoot如何用java生成靜態(tài)html,文章圍繞主題展開詳細的內(nèi)容介紹,需要的朋友可以參考一下
SpringBoot集成Freemarker
- 主要特征:靜態(tài)頁面,無接口交互
- 數(shù)據(jù)實時性不高且體量小的網(wǎng)站可采用生成靜態(tài)html的形式
- 數(shù)據(jù)提前渲染至html內(nèi),若發(fā)生數(shù)據(jù)更新,則重新渲染數(shù)據(jù)
- CDN加速讓網(wǎng)站不再龜速
1. 引入Maven依賴
<dependency> ? ? <groupId>org.freemarker</groupId> ? ? <artifactId>freemarker</artifactId> ? ? <version>2.3.28</version> </dependency>
2. 創(chuàng)建ftl
<html>
? ? <head>
? ? ? ? <title>啦啦啦啦啦</title>
? ? </head>
? ? <body>
? ? ? ? <h1>俠客行</h1>
? ? ? ? <p>${author!}</P>
? ? ? ? <#if (poem?size)!=0>
? ? ? ? ? ? <#list poem as item>
? ? ? ? ? ? ? ? <p>${item.first!}${item.second!}</p></br>
? ? ? ? ? ? </#list>
? ? ? ? </#if>
? ? </body>
</html>3. 創(chuàng)建freeMarker工具類
@Slf4j
@Component
public class?FreeMarkerUtil {
? ? private static?Configuration?config;
? ? private static?String?serverPath;
? ? @Value("${spring.servlet.multipart.location:D:/static/}")
? ? public void?setServerPath(String serverPath) {
? ? ? ? FreeMarkerUtil.serverPath?= serverPath;
? ? }
/**
*?通過freemarker生成靜態(tài)HTML頁面
*?@param?templateName?模版名稱
*?@param?targetFileName?生成后的文件名
*?@param?ftlPath模板路徑
*?@param?htmlPathhtml路徑
*?@param?mapfreemarker生成的數(shù)據(jù)都存儲在MAP中,
*/
public static void?createHtml(String templateName,?String targetFileName,?String ftlPath,?String htmlPath,?Map<String,?Object> map) {
try{? ??
? ? //創(chuàng)建fm的配置
? ? config?=?new?Configuration();
? ? //指定默認編碼格式
? ? config.setDefaultEncoding("UTF-8");
? ? //設(shè)置模版文件的路徑
? ? config.setDirectoryForTemplateLoading(new?File(serverPath+ftlPath));
? ? //獲得模版包
? ? Template template =?config.getTemplate(templateName);
? ? //從參數(shù)文件中獲取指定輸出路徑
? ? String path =?serverPath+htmlPath;
? ? //生成的靜態(tài)頁存放路徑如果不存在就創(chuàng)建
? ? File file =?null;
? ? file=new?File(path);
? ? if?(!file.exists()){
? ? ? ? file.mkdirs();
? ? }
? ? //定義輸出流,注意必須指定編碼
? ? Writer writer =?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(new?File(path+"/"+targetFileName)),?UTF_8));
? ? //生成模版
? ? template.process(map,?writer);
}catch?(Exception e){
? ? log.error("生成異常:{}",e);
}
}4. 編寫Java的代碼
構(gòu)造實體類,通過freemarker將實體類的信息渲染至html
@GetMapping("test")
public?Object?test() {
? ? Map<String,Object> map =?new?HashMap<>(16);
? ? List<Poem> list =?new?ArrayList<>();
? ? list.add(new?Poem("趙客縵胡纓,",?"吳鉤霜雪明。"));
? ? list.add(new?Poem("銀鞍照白馬,",?"颯沓如流星。"));
? ? list.add(new?Poem("十步殺一人,",?"千里不留行。"));
? ? list.add(new?Poem("事了拂衣去,",?"深藏身與名。"));
? ? map.put("author","李白");
? ? map.put("poem",list);
? ? FreeMarkerUtil.createHtml("poem.ftl","poem.html","俠客行/","俠客行/",map);
? ? return?BackMessage.ok(map);
}實體類:
@Data
public class?Poem {
? ? private?String?first;
? ? private?String?second;
? ? public?Poem(String first,?String second) {
? ? ? ? this.first?= first;
? ? ? ? this.second?= second;
? ? }
}5. Html輸出
<html> <head> ? ? <title>啦啦啦啦啦</title> </head> <body> ? ? <h1>俠客行</h1> ? ? <p>李白</P> ? ? <p>趙客縵胡纓,吳鉤霜雪明。</p></br> ? ? <p>銀鞍照白馬,颯沓如流星。</p></br> ? ? <p>十步殺一人,千里不留行。</p></br> ? ? <p>事了拂衣去,深藏身與名。</p></br> </body> </html>
到此這篇關(guān)于SpringBoot如何用java生成靜態(tài)html的文章就介紹到這了,更多相關(guān)java生成靜態(tài)html內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis?Plus實現(xiàn)中文排序的兩種有效方法
在MyBatis?Plus項目開發(fā)中,針對中文數(shù)據(jù)的排序需求是一個常見的挑戰(zhàn),尤其是在需要按照拼音或特定語言邏輯排序時,本文整合了兩種有效的方法,旨在幫助開發(fā)者克服MyBatis?Plus在處理中文排序時遇到的障礙,需要的朋友可以參考下2024-08-08
Java實現(xiàn)飛機大戰(zhàn)游戲?附完整源碼
這篇文章主要介紹了Java實現(xiàn)飛機大戰(zhàn)游戲,本文給大家分享完整源代碼和效果圖展示,對java飛機大戰(zhàn)游戲?qū)崿F(xiàn)代碼感興趣的朋友一起看看吧2022-05-05

