Spring-boot集成freemarker入門(mén)|實(shí)踐
介紹
官方介紹 https://freemarker.apache.org/
百度百科:https://baike.baidu.com/item/freemarker/9489366?fr=aladdin
關(guān)于介紹本文就不重復(fù)敘述了
下面直接開(kāi)始實(shí)踐做瞪!
Freemarker + Springboot Maven配置基礎(chǔ)工程
創(chuàng)建一個(gè)maven項(xiàng)目
spring-boot項(xiàng)目中添加依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐freemarker</artifactId>
</dependency>
</dependencies>
在resource目錄下添加配置文件
server:
port: 8088 # 服務(wù)端口
spring:
application:
name: test-freemarker
freemarker:
cache: false #關(guān)閉模板緩存,方便測(cè)試
settings:
template_update_delay: 0 #檢查模板更新延遲時(shí)間柴灯,設(shè)置為0表示立即檢查偏窝,如果時(shí)間大于0會(huì)有緩存不方便進(jìn)行模板測(cè)試
創(chuàng)建數(shù)據(jù)模型類(lèi)
package com.dsdj.test.freemarker.model;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
import java.util.List;
/**
* model
*
* @author dsdj
* @version 1.0
* @className Student
* @date 2019/2/12 9:09
**/
@Data
@ToString
public class Student {
private String name;//姓名
private int age;//年齡
private Date birthday;//生日
private Float money;//錢(qián)包
private List<Student> friends;//朋友列表
private Student bestFriend;//最好的朋友
}
這里使用了lombok需要引入下面的依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
創(chuàng)建模板
在 src/main/resources下創(chuàng)建templates曼尊,此目錄為freemarker的默認(rèn)模板存放目錄。
在templates下創(chuàng)建模板文件demo.ftl.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf‐8">
<title>Hello World!</title>
</head>
<body>
Hello ${name}!
</body>
</html>
模板中的${name}最終會(huì)被freemarker替換成具體的數(shù)據(jù)阅虫。
創(chuàng)建controller
創(chuàng)建Controller類(lèi)粘都,向Map中添加name,最后返回模板文件甥温。
添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
創(chuàng)建啟動(dòng)類(lèi)
package com.dsdj.test.freemarker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* spring boot 啟動(dòng)類(lèi)
*
* @author dsdj
* @version 1.0
* @className FreemarkerTestApplication
* @date 2019/2/12 9:41
**/
@SpringBootApplication
public class FreemarkerTestApplication {
public static void main(String[] args) {
SpringApplication.run(FreemarkerTestApplication.class,args);
}
}
啟動(dòng)測(cè)試
FreeMarker基礎(chǔ)
關(guān)于freemarket的知識(shí)點(diǎn)锻煌,主要是掌握一起下知識(shí)
總體結(jié)構(gòu)
指令(幾個(gè)核心指令)
表達(dá)式
插值
讀者可以參考下面的教程
http://freemarker.foofun.cn/dgui_template_overallstructure.html
這個(gè)教程是翻譯自官方的文檔×蓿可以快速瀏覽一遍炼幔,不懂再去查閱。
在掌握了基礎(chǔ)的語(yǔ)法之后史简,下面進(jìn)行靜態(tài)化實(shí)踐乃秀。
freemarker靜態(tài)化實(shí)踐
使用模板文件靜態(tài)化
創(chuàng)建測(cè)試類(lèi)
package com.dsdj.test.freemarkert;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
/**
* TODO
*
* @author dsdj
* @version 1.0
* @className TestFreemarker
* @date 2019/2/12 10:30
**/
public class TestFreemarker {
@Test
public void testGenerateHtml() throws IOException, TemplateException, URISyntaxException {
// 創(chuàng)建配置類(lèi)
Configuration configuration = new Configuration(Configuration.getVersion());
// 設(shè)置模板路徑 toURI()防止路徑出現(xiàn)空格
String classpath = this.getClass().getResource("/").toURI().getPath();
configuration.setDirectoryForTemplateLoading(new File(classpath+"/templates/"));
// 設(shè)置字符集
configuration.setDefaultEncoding("utf-8");
// 加載模板
Template template = configuration.getTemplate("demo1.ftl");
// 數(shù)據(jù)模型
Map<String,Object> map = new HashMap<>();
map.put("name", "靜態(tài)化測(cè)試");
// 靜態(tài)化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
// 打印靜態(tài)化內(nèi)容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
// 輸出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("demo1.html"));
int copy = IOUtils.copy(inputStream, fileOutputStream);
}
}
測(cè)試結(jié)果
使用模板字符串靜態(tài)化
測(cè)試方法
@Test
public void testGenerateHtmlByString() throws IOException, TemplateException {
// 創(chuàng)建配置類(lèi)
Configuration configuration = new Configuration(Configuration.getVersion());
// 測(cè)試模板內(nèi)容
String templateString="" +
"<html>\n" +
" <head></head>\n" +
" <body>\n" +
" 名稱(chēng):${name}\n" +
" </body>\n" +
"</html>";
// 模板加載器
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate("template",templateString);
configuration.setTemplateLoader(stringTemplateLoader);
// 得到模板
Template template = configuration.getTemplate("template","utf-8");
// 數(shù)據(jù)模型
Map<String,Object> map = new HashMap<>();
map.put("name","使用模板字符串靜態(tài)化");
// 靜態(tài)化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
// 打印靜態(tài)化內(nèi)容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
// 輸出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("demo1.html"));
int copy = IOUtils.copy(inputStream, fileOutputStream);
}
以上就是freemarker的基本使用,但看到這里我們肯定有很多疑問(wèn)圆兵。下面進(jìn)行總結(jié)
總結(jié)
為什么可以直接直接跳轉(zhuǎn)跺讯?
模板靜態(tài)化如何在場(chǎng)景下使用?