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的依賴(lài):
<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)類(lèi)統(tǒng)計(jì)目錄下新建utils包,在其中新建Resources類(lèi)(此處使用配置文件引入相關(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è)類(lèi)是一個(gè)讀取配置文件的類(lèi)
@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類(lèi):
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,其中的很多方法也很類(lèi)似,我們這里返回的字符串就是freemarker模板的路徑,不用寫(xiě)后綴,因?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>的寫(xiě)法,這個(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)文章
Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突
這篇文章主要介紹了 Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突的相關(guān)資料,需要的朋友可以參考下2017-01-01
springboot使用國(guó)產(chǎn)加密算法方式,sm2和sm3加解密demo
這篇文章主要介紹了springboot使用國(guó)產(chǎn)加密算法方式,sm2和sm3加解密demo,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
深入解析Spring Cloud內(nèi)置的Zuul過(guò)濾器
這篇文章主要給大家深入的介紹了Spring Cloud內(nèi)置的Zuul過(guò)濾器的相關(guān)資料,文中給大家介紹的很詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02
kafka內(nèi)外網(wǎng)訪問(wèn)配置方式
這篇文章主要介紹了kafka內(nèi)外網(wǎng)訪問(wèn)配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring AOP的底層實(shí)現(xiàn)方式-代理模式
這篇文章主要介紹了Spring AOP的底層實(shí)現(xiàn)方式-代理模式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug
這篇文章主要介紹了IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug,本文通過(guò)實(shí)例代碼圖文的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
java?kafka如何動(dòng)態(tài)設(shè)置用戶讀寫(xiě)權(quán)限
這篇文章主要介紹了java?kafka如何動(dòng)態(tài)設(shè)置用戶讀寫(xiě)權(quán)限問(wèn)題,具有很好的參考家價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
這篇文章主要介紹了springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

