新聞中心
今天就跟大家聊聊有關freemarker如何在Spring Boot項目中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)網(wǎng)站建設服務商,為中小企業(yè)提供做網(wǎng)站、成都做網(wǎng)站服務,網(wǎng)站設計,網(wǎng)站托管等一站式綜合服務型公司,專業(yè)打造企業(yè)形象網(wǎng)站,讓您在眾多競爭對手中脫穎而出創(chuàng)新互聯(lián)。
(1) freemarker介紹;
FreeMarker是一款模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁、電子郵件、配置文件、源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發(fā)產(chǎn)品的組件。
(2) 新建spring-boot-freeMarker工程;
我們新建一個maven工程,取名為:spring-boot-freemarker
(3) 在pom.xml引入相關依賴;
這里使用freeMarker需要引入相關依賴包:spring-boot-starter-freemarker,
4.0.0 com.kfit spring-boot-velocity 0.0.1-SNAPSHOT jar spring-boot-velocity http://maven.apache.org UTF-8 1.8 org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE junit junit test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-freemarker
(4) 編寫啟動類;
啟動類沒有什么特別之處,不過多介紹,請看代碼:
package com.kfit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @author Angel --守護天使 * @version v.0.1 * @date 2016年10月4日 */ @SpringBootApplication public class App { publicstaticvoid main(String[] args) { SpringApplication.run(App.class, args); } }
(5) 編寫模板文件hello.ftl;
編寫一個hello.ftl文件,此文件的路徑在src/main/resources/templates下,其中hello.ftl文件的內(nèi)容如下:
welcome ${name} to freemarker!
(6) 編寫訪問類HelloController;
有了模板文件之后,我們需要有個Controller控制類,能夠訪問到hello.ftl文件,這里也很簡單,具體看如下代碼:
package com.kfit.demo.web; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 測試velocity; * @author Angel --守護天使 * @version v.0.1 * @date 2016年10月4日 */ @Controller public class HelloController { @RequestMapping("/hello") public String hello(Mapmap){ map.put("name", "[Angel -- 守護天使]"); return "hello"; } }
(7) 測試;
好了,到這里,我們就可以啟動我們的程序進行測試了,訪問地址:
http://127.0.0.1:8080/hello ,如果你在瀏覽器中看到如下信息:
welcome [Angel -- 守護天使] to freemarker!
那么說明你的demo ok 了。
(8) freemarker配置;
在spring boot的application.properties屬性文件中為freemarker提供了一些常用的配置,如下:
######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.suffix=.ftl #spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved
(9) freemarker常用語法;
freemarker的語法并不是本節(jié)的重點,這里還是簡單的介紹下幾個常用的if else,list;
首先我們改造下HelloController的hello方法
@RequestMapping("/hello") public String hello(Mapmap){ map.put("name", "[Angel -- 守護天使]"); map.put("gender",1);//gender:性別,1:男;0:女; List
這里我們返回了gender和friends的列表;
接下來我們看看怎么在freemarker進行展示呢?
Hello World! welcome ${name} to freemarker!
性別: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 #if>
我的好友:
<#list friends as item> 姓名:${item.name} , 年齡${item.age}
#list>
(10) freemarker layout
freemarker layout主要處理具有相同內(nèi)容的頁面,比如每個網(wǎng)站的header和footer頁面。
freemarker 的布局主要常見的兩種方式是#import(“文件路徑”)和#include(“文件路徑”),其中import和include的區(qū)別在于,include常用于公共部分的頁面,如果要使用<#assign username=“張三”>涉及到內(nèi)部函數(shù)以及變量聲明之類的,使用import進行導入,如果在import中的頁面含有頁面當前將不會進行渲染。 我們編寫一個header和footer,其中的header使用include引入,footer頁面也使用include引入。(當然freemarker 還有別的布局方式,這里只是介紹一種,請自行學習研究)
header.ftl內(nèi)容:
This is a header,welcome ${name} to my web site!
footer.ftl內(nèi)容:
修改hello.ftl:
Hello World! <#include "/header.ftl" >welcome ${name} to freemarker!
性別: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 #if>
我的好友:
<#list friends as item> 姓名:${item.name} , 年齡${item.age}
#list> <#include "/footer.ftl" >
到這里就ok了,我們訪問/hello頁面,應該會看到如下圖的效果:
看完上述內(nèi)容,你們對freemarker如何在Spring Boot項目中使用有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享題目:freemarker如何在SpringBoot項目中使用
本文網(wǎng)址:http://fisionsoft.com.cn/article/josgdd.html