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

首先,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)閉緩存,及時(shí)刷新,上線生產(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>
我們?cè)趓esources下新建resource.properties:
com.haozz.opensource.name=wangshu com.haozz.opensource.website=www.haozz.top:18158/ com.haozz.opensource.language=chinese
在SpringBoot啟動(dòng)類統(tǒng)計(jì)目錄下新建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;
//表示這個(gè)類是一個(gè)讀取配置文件的類
@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模板的路徑,不用寫后綴,因?yàn)榕渲梦募幸呀?jīng)指定了后綴為.ftl
瀏覽器發(fā)起請(qǐng)求,得到結(jié)果:

這樣,SpringBoot整合freemarker就好了。
我們?cè)賮?lái)試一下表格的形式。
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>
瀏覽器請(qǐng)求:

可以看到,在center.ftl中,我們使用了<#list users as user>的寫法,這個(gè)相當(dāng)于jstl表達(dá)式中的c:forEach。而users集合我們?cè)贔reeMarkerCtrl已經(jīng)初始化了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Springboot整合freemarker和相應(yīng)的語(yǔ)法詳解
- springboot整合freemarker代碼自動(dòng)生成器
- Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程
- SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁(yè)面靜態(tài)化示例詳解
- Springboot整合freemarker 404問(wèn)題解決方案
- springboot 整合 freemarker代碼實(shí)例
- springboot整合freemarker詳解
- Springboot整合FreeMarker的實(shí)現(xià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如何實(shí)現(xiàn)多個(gè)線程之間共享數(shù)據(jù)
這篇文章主要介紹了Java如何實(shí)現(xiàn)多個(gè)線程之間共享數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java無(wú)限遞歸遍歷json對(duì)象問(wèn)題
這篇文章主要介紹了java無(wú)限遞歸遍歷json對(duì)象問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀
這篇文章主要介紹了Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀,線程池是一種多線程處理形式,處理過(guò)程中將任務(wù)添加到隊(duì)列,然后在創(chuàng)建線程后自動(dòng)啟動(dòng)這些任務(wù),線程池線程都是后臺(tái)線程,需要的朋友可以參考下2023-12-12
使用Feign消費(fèi)服務(wù)時(shí)POST/GET請(qǐng)求方式詳解
這篇文章主要介紹了使用Feign消費(fèi)服務(wù)時(shí)POST/GET請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2018-05-05

