SpringBoot整合Freemarker實(shí)現(xiàn)頁(yè)面靜態(tài)化的詳細(xì)步驟
第一步:創(chuàng)建項(xiàng)目添加依賴:
<!--web和actuator(圖形監(jiān)控用)基本上都是一起出現(xiàn)的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
第二步:修改application.yml文件:
spring:
freemarker:
charset: UTF-8 #設(shè)定Template的編碼
suffix: .ftl #后綴名
template-loader-path: classpath:/templates/ #模板加載路徑,多個(gè)以逗號(hào)分隔,默認(rèn): [“classpath:/templates/”]
cache: false #緩存配置,是否開(kāi)啟template caching
enabled: true #是否允許mvc使用freemarker
第三步:在resources/templates目錄下創(chuàng)建模板文件index.ftl:
<html>
<head>
<title>${title}</title>
</head>
<body>
<h2>${msg}</h2>
</body>
</html>
第四步:創(chuàng)建代碼靜態(tài)化工具類:
@Component
public class GenUtil {
//創(chuàng)建Freemarker配置實(shí)例
@Resource
private Configuration configuration;
/**
* 根據(jù)模板,利用提供的數(shù)據(jù),生成文件
*
* @param sourceFile 模板文件,帶路徑
* @param data 數(shù)據(jù)
* @param aimFile 最終生成的文件,若不帶路徑,則生成到當(dāng)前項(xiàng)目的根目錄中
*/
public void gen(String sourceFile, String aimFile, Map<String, Object> data) {
try {
//加載模板文件
Template template = configuration.getTemplate(sourceFile);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(aimFile), StandardCharsets.UTF_8));
template.process(data, out);
out.flush();
out.close();
} catch (IOException | TemplateException e) {
e.printStackTrace();
}
}
}
第五步:靜態(tài)化測(cè)試
@SpringBootTest
public class GenTest {
@Resource
private GenUtil genUtil;
@Test
void fun(){
Map<String, Object> map = new HashMap<>();
map.put("title", "首頁(yè)");
map.put("msg", "好好學(xué)習(xí),天天向上!");
FreemarkerUtil.execute("index.ftl", "haha.html", map);
}
}
測(cè)試
運(yùn)行測(cè)試代碼發(fā)現(xiàn)在當(dāng)前項(xiàng)目根目錄下生成了一個(gè)haha.html的文件。
到此這篇關(guān)于SpringBoot整合Freemarker實(shí)現(xiàn)頁(yè)面靜態(tài)化的文章就介紹到這了,更多相關(guān)SpringBoot整合Freemarker內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Spring配置文件中bean的property屬性中的name出錯(cuò)問(wèn)題
這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
springboot實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的示例代碼
項(xiàng)目里面有用到用戶手機(jī)號(hào)注冊(cè)發(fā)短信功能,本文主要介紹了springboot實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
java實(shí)現(xiàn)優(yōu)酷視頻地址解析示例代碼分享
最近做了一個(gè)在線視頻的下載器,需要解析youku的視頻,獲得真正的視頻地址,現(xiàn)在把解析過(guò)程記錄下來(lái)以供參考2014-01-01
idea推送項(xiàng)目到gitee中的創(chuàng)建方法
這篇文章主要介紹了idea推送項(xiàng)目到gitee中的創(chuàng)建方法,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
springMVC中@RequestParam和@RequestPart的區(qū)別
本文主要介紹了springMVC中@RequestParam和@RequestPart的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
Maven發(fā)布封裝到中央倉(cāng)庫(kù)時(shí)候報(bào)錯(cuò):no default secret key
這篇文章主要介紹了Maven發(fā)布封裝到中央倉(cāng)庫(kù)時(shí)候報(bào)錯(cuò):no default secret key,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

