這篇文章主要介紹“FreeMarker的原理和應(yīng)用”,在日常操作中,相信很多人在FreeMarker的原理和應(yīng)用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”FreeMarker的原理和應(yīng)用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

10年的武城網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整武城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“武城網(wǎng)站設(shè)計”,“武城網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
FreeMarker是一個用Java開發(fā)的模版引擎,即一種基于模版和要改變的數(shù)據(jù),并用來生成輸出文本(HTML網(wǎng)頁,電子郵件,配置文件,源代碼等)的通用工具。它不是面向最終用戶的,而是一個java類庫,是一款程序員可以嵌入他們所開發(fā)產(chǎn)品的組件。
FreeMarker并不關(guān)心數(shù)據(jù)的來源,只是根據(jù)模版的內(nèi)容,將數(shù)據(jù)模版在模版中顯示并輸出文件(通常是html,也可以生成其他格式的文本文件)
數(shù)據(jù)模型:數(shù)據(jù)模型在java中可以是基本類型也可以說List,Map,Pojo等復(fù)雜類型
快速入門:
一、添加依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.1.0</version> </dependency> </dependencies>
二、編寫啟動類
@SpringBootApplication
public class FreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(FreemarkerApplication.class);
}
}三、編寫實體類
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private int age;
private Date birthday;
private Float money;
private List<Student> friends;
private Student bestFriends;
}四、在resources下創(chuàng)建templates文件夾,創(chuàng)建模版文件 test1.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello ${name}!
</body>
</html>五、編寫controller
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
@RequestMapping("/test1")
public String freemarker(Map<String ,Object> map){
map.put("name","cxy");
return "test1";
}
}六、啟動測試
freemarker基礎(chǔ)知識:
1、注釋,即<#-- 內(nèi)容 -->,介于其之間的內(nèi)容會被freemarker忽略
2、插值,即${},會被freemarker用真實值替代
3、FTL,和html標記類似,名字前加#予以區(qū)別,freemarker會解析標簽中的表達式或邏輯
4、文本,僅文本信息,直接輸出內(nèi)容
List指令:在模版中遍歷數(shù)據(jù)
<table>
<#list stus as stu>
<tr>
<td>${stu.index}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.money}</td>
</tr>
</#list>
</table>Map指令
<table>
<#list stuMap ?keys as k>
<tr>
<td>${k.index - 1}</td>
<td>${stuMap[k].name}</td>
<td>${stuMap[k].age}</td>
<td>${stuMap[k].money}</td>
</tr>
</#list>
</table>if指令
<td<#if stuMap[k].name =="cxy" ></#if>>${k.index - 1}</td>空值處理:判斷某變量是否存在使用??
<#if stuMap??>
<tr>
<td<#if stuMap[k].name =="cxy" ></#if>>${k.index - 1}</td>
<td>${stuMap[k].name}</td>
<td>${stuMap[k].age}</td>
<td>${stuMap[k].money}</td>
</tr>
</#if>注意:缺失變量默認值使用!指定一個默認值,當變量為空時顯示默認值。
內(nèi)建函數(shù)
1、集合大小:${集合名?size}
2、日期格式化:
${today?date},顯示年月日
${today?time},顯示時分秒
${today?datetime},顯示日期加時間
${today?string(yyyy-MM-dd)},按字符格式顯示
3、${money?c}將數(shù)字以字符串形式顯示,不然每三位添加一個逗號
4、將字符串轉(zhuǎn)成對象 <#assign data=text?eval/>
靜態(tài)化實現(xiàn)
1、使用模版文件靜態(tài)化
2、使用模版字符串靜態(tài)化
模版靜態(tài)化測試代碼
public class FreemarkerTest {
@Test
public void testGenerateHtmk() throws IOException, TemplateException {
Configuration configuration = new Configuration(Configuration.getVersion());
URL resource = this.getClass().getClassLoader().getResource("templates/");
String path = resource.getPath();
configuration.setDirectoryForTemplateLoading(new File(path));
configuration.setDefaultEncoding("utf-8");
Template template = configuration.getTemplate("test1.ftl");
System.out.println(template);
Map<String,Object> map = new HashMap<>();
map.put("name","黑馬程序員");
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(content);
}
}到此,關(guān)于“FreeMarker的原理和應(yīng)用”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當前標題:FreeMarker的原理和應(yīng)用
分享網(wǎng)址:http://chinadenli.net/article42/ipcpec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、虛擬主機、網(wǎng)站設(shè)計、網(wǎng)站收錄、外貿(mào)建站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)