SpringBoot如何用java生成靜態(tài)html
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(); ? ? //指定默認(rèn)編碼格式 ? ? 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)文章
帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01詳解Spring Cloud Stream使用延遲消息實現(xiàn)定時任務(wù)(RabbitMQ)
這篇文章主要介紹了詳解Spring Cloud Stream使用延遲消息實現(xiàn)定時任務(wù)(RabbitMQ),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Java動態(tài)線程池插件dynamic-tp集成zookeeper
ZooKeeper是一個分布式的,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個開源的實現(xiàn),是Hadoop和Hbase的重要組件。它是一個為分布式應(yīng)用提供一致性的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等2023-03-03IntelliJ IDEA 小技巧之Bookmark(書簽)的使用
這篇文章主要介紹了IntelliJ IDEA 小技巧之Bookmark(書簽)的使用,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07淺談Spring Cloud zuul http請求轉(zhuǎn)發(fā)原理
這篇文章主要介紹了淺談Spring Cloud zuul http請求轉(zhuǎn)發(fā)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Springboot+rabbitmq實現(xiàn)延時隊列的兩種方式
這篇文章主要介紹了Springboot+rabbitmq實現(xiàn)延時隊列的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復(fù)提交校驗
本文主要介紹了SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復(fù)提交校驗,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01