今天小編就爲大家分享一篇關於SpringBoot整合freemarker的講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
freemarker和thymeleaf是模板引擎。在早前我們使用Struts或者SpringMVC等框架的時候,使用的都是jsp,jsp的本質其實就是一個Servlet,其中的數據需要在後端進行渲染,然後再在客戶端顯示,效率比較低下。而模板引擎恰恰相反,其中的數據渲染是在客戶端,效率方面比較理想一點。前後端不分離的話用模板引擎比較好,前後端分離的話其實用處並不大很大。Spring官方比較推薦的是thymeleaf,其文件後綴是html。本篇文章我們主要來看看SpringBoot整合freemarker,SpringBoot整合thymeleaf我們將在後面的文章中講解。
先來看一下項目文件目錄:
首先,pom.xml
中導入freemarker
的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
在application.properties
(或yml)配置文件中加入freemarker
相關配置:
# freemarker靜態資源配置 # 設定ftl文件路徑 spring.freemarker.tempalte-loader-path=classpath:/templates # 關閉緩存,及時刷新,上線生產環境需要修改爲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(上面配置文件中已經指定了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啓動類統計目錄下新建utils包,在其中新建Resources類(此處使用配置文件引入相關數據):
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就相當於SpringMVC中的ModelAndView,其中的很多方法也很類似,我們這裏返回的字符串就是freemarker模板的路徑,不用寫後綴,因爲配置文件中已經指定了後綴爲.ftl
瀏覽器發起請求,得到結果:
這樣,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; }/*設置邊框*/ </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>
的寫法,這個相當於jstl表達式中的c:forEach。而users集合我們在FreeMarkerCtrl已經初始化了。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接