SpringBoot整合freemarker的講解
freemarker和thymeleaf是模板引擎。在早前我們使用Struts或者SpringMVC等框架的時候,使用的都是jsp,jsp的本質(zhì)其實就是一個Servlet,其中的數(shù)據(jù)需要在后端進(jìn)行渲染,然后再在客戶端顯示,效率比較低下。而模板引擎恰恰相反,其中的數(shù)據(jù)渲染是在客戶端,效率方面比較理想一點。前后端不分離的話用模板引擎比較好,前后端分離的話其實用處并不大很大。Spring官方比較推薦的是thymeleaf,其文件后綴是html。本篇文章我們主要來看看SpringBoot整合freemarker,SpringBoot整合thymeleaf我們將在后面的文章中講解。
先來看一下項目文件目錄:

首先,pom.xml中導(dǎo)入freemarker的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
在application.properties(或yml)配置文件中加入freemarker相關(guān)配置:
# freemarker靜態(tài)資源配置 # 設(shè)定ftl文件路徑 spring.freemarker.tempalte-loader-path=classpath:/templates # 關(guān)閉緩存,及時刷新,上線生產(chǎn)環(huán)境需要修改為true spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=true spring.freemarker.expose-session-attributes=true spring.freemarker.request-context-attribute=request spring.freemarker.suffix=.ftl
這里指定了freemarker文件的路徑是classpath/templates,在resources文件夾下的templates新建freemarker文件夾,并且在其中新建index.ftl(上面配置文件中已經(jīng)指定了freemarker模板的文件后綴為ftl):
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
FreeMarker模板引擎
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>
我們在resources下新建resource.properties:
com.haozz.opensource.name=wangshu com.haozz.opensource.website=www.haozz.top:18158/ com.haozz.opensource.language=chinese
在SpringBoot啟動類統(tǒng)計目錄下新建utils包,在其中新建Resources類(此處使用配置文件引入相關(guān)數(shù)據(jù)):
package com.haozz.freemarkerdemo.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//表示這個類是一個讀取配置文件的類
@Configuration
//指定配置的一些屬性,其中的prefix表示前綴
@ConfigurationProperties(prefix = "com.haozz.opensource")
//指定所讀取的配置文件的路徑
@PropertySource(value = "classpath:resource.properties")
public class Resource {
private String name;
private String website;
private String language;
//...setter and getter
}
新建Controller包,新建FreeMarkerCtrl類:
package com.haozz.freemarkerdemo.controller;
import com.haozz.freemarkerdemo.utils.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/ftl")
public class FreeMarkerCtrl {
@Autowired
private Resource resource;
@RequestMapping(value = "index")
public String index(ModelMap map){
map.addAttribute("resource",resource);
return "freemarker/index";
}
}
這里的ModelMap就相當(dāng)于SpringMVC中的ModelAndView,其中的很多方法也很類似,我們這里返回的字符串就是freemarker模板的路徑,不用寫后綴,因為配置文件中已經(jīng)指定了后綴為.ftl
瀏覽器發(fā)起請求,得到結(jié)果:

這樣,SpringBoot整合freemarker就好了。
我們再來試一下表格的形式。
FreeMarkerCtrl中新增方法:
@RequestMapping(value ="center")
public String center(ModelMap map){
map.put("users",parseUsers());
map.put("title","用戶列表");
return "freemarker/center/center";
}
private List<Map> parseUsers(){
List<Map> list= new ArrayList<>();
for(int i=0;i<10;i++){
Map map= new HashMap();
map.put("name","kevin_"+i);
map.put("age",10+i);
map.put("phone","1860291105"+i);
list.add(map);
}
return list;
}
在resources/templates/freemarker下新建center文件夾,新建center.ftl:
<html lang="zh-CN">
<head>
<meta charset="UTF-8"/>
<title>${title}</title>
<style>
table {
width: 50%;
font-size: .938em;
border-collapse: collapse;/*邊框合并*/
}
th {
text-align: left;
padding: .5em .5em;
font-weight: bold;
background: #66677c;color: #fff;
}
td {
padding: .5em .5em;
border-bottom: solid 1px #ccc;
}
table,table tr th, table tr td { border:1px solid #0094ff; }/*設(shè)置邊框*/
</style>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Phone</th>
</tr>
<#list users as user>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.phone}</td>
</tr>
</#list>
</table>
</body>
</html>
瀏覽器請求:

可以看到,在center.ftl中,我們使用了<#list users as user>的寫法,這個相當(dāng)于jstl表達(dá)式中的c:forEach。而users集合我們在FreeMarkerCtrl已經(jīng)初始化了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Java基礎(chǔ)之隱式轉(zhuǎn)換vs強(qiáng)制轉(zhuǎn)換
這篇文章主要介紹了Java基礎(chǔ)之隱式轉(zhuǎn)換vs強(qiáng)制轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2015-12-12
JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)...2006-12-12
Java如何實現(xiàn)多個線程之間共享數(shù)據(jù)
這篇文章主要介紹了Java如何實現(xiàn)多個線程之間共享數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀
這篇文章主要介紹了Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀,線程池是一種多線程處理形式,處理過程中將任務(wù)添加到隊列,然后在創(chuàng)建線程后自動啟動這些任務(wù),線程池線程都是后臺線程,需要的朋友可以參考下2023-12-12
使用Feign消費服務(wù)時POST/GET請求方式詳解
這篇文章主要介紹了使用Feign消費服務(wù)時POST/GET請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Boot利用@Async異步調(diào)用:使用Future及定義超時詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2018-05-05

