FreeMarker是一款模板引擎:一種基于模板的、用來生成輸出文本(任何來自于HTML格式的文本用來自動生成源代碼)的通用工具。它是為java程序員提供的一個開發包或者說是類庫。它不是面向最終用戶,而是為程序員提供的可以嵌入他們開發產品的一款應用程序。 特點 功能 基礎 概要、關鍵字 建議 前言
FreeMarker是一款模板引擎:一種基于模板的、用來生成輸出文本(任何來自于HTML格式的文本用來自動生成源代碼)的通用工具。它是為Java程序員提供的一個開發包或者說是類庫。它不是面向最終用戶,而是為程序員提供的可以嵌入他們開發產品的一款應用程序。
FreeMarker的設計實際上是被用來生成HTML網頁,尤其是通過基于實現了MVC(Model View Controller,模型-視圖-控制器)模式的Servlet應用程序。使用MVC模式的動態網頁的構思使得你可以將前端設計者(編寫HTML)從程序員中分離出來。所有人各司其職,發揮其擅長的一面。網頁設計師可以改寫頁面的顯示效果而不受程序員編譯代碼的影響,因為應用程序的邏輯(Java程序)和頁面設計(FreeMarker模板)已經分開了。頁面模板代碼不會受到復雜的程序代碼影響。這種分離的思想即便對一個程序員和頁面設計師是同一個人的項目來說都是非常有用的,因為分離使得代碼保持簡潔而且便于維護。
FreeMarker不是Web應用框架。它是Web應用框架中的一個適用的組件。 第1章 入門 1.2 模板+數據模型=輸出 1.3 數據模型一覽 1.4 模板一覽 1.4.1 簡介
FTL tags標簽:FreeMarker模板的語言標簽。一般以符合#
開頭,用戶自定義的FTL標簽使用@
代替#
。
Comments注釋:FreeMarker的注釋和HTML的注釋相似,但是它用<#--
和-->
來分隔。
directives指令:就是所指的FTL標簽。 1.4.2 指令示例 1.4.2.1 if指令
假設你只想向你的老板Big Joe(而不是其他人)問好,就可以這樣做:
<h1>Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!</h1>
使用<#else>標簽:
<#if animals.python.PRice < animals.elephant.price> Pythons are cheaper than elephants today.<#else> Pythons are not cheaper than elephants today.</#if>
如果變量本身就是布爾值,可以直接作為if的條件;
<#if animals.python.protected> Warniing! Pythons are protected animals!</#if>
實例/FreeMarker-hello-web/src/main/java/org/yejq/fre/model/Animal.java
public class Animal { private String name; private double price; private boolean protect; 。。。}
/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises.java
public void testIf(Model model){ model.addAttribute("user", "Big Joe"); Map<String, Animal> animals = new HashMap<String, Animal>(); animals.put("python", new Animal("python", 300, true)); animals.put("elephant", new Animal("elephant", 400, false)); model.addAttribute("animals", animals); }
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/if.ftl
<!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <title>If指令</title></head><body> <h1>Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if></h1> <p> <#--大于號兩邊要加括號括起來,否則會以為是結束標簽 --> <#if (animals.python.price > animals.elephant.price)> python.price > elephant.price <#else> python.price <= elephant.price </#if> </p> <p> <#if animals.python.protect> python.protect = true; </#if> </p></body></html>
測試: http://localhost/test/2/if/testIf1.4.2.2 list指令
當需求遍歷集合的內容時,list指令是非常好用的。
<#list animals as being> <tr><td>${being.name}<td>${being.price} Euros</#list>
實例/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises.java
public void testList(Model model){ List<Animal> animals = new ArrayList<Animal>(); animals.add(new Animal("python", 300, true)); animals.add(new Animal("elephant", 400, false)); model.addAttribute("animals", animals); }
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/list.ftl
<h3>list指令</h3> <table border=1> <#list animals as animal> <tr> <#-- boolean類型要設置默認輸出值,否則報錯 --> <td>${animal.name}, ${animal.price}, ${animal.protect?c}</td> </tr> </#list> </table>
測試:
http://localhost/test/2/list/testList1.4.2.3 include指令
在當前模板中插入其他文件的內容。
copyright_footer.html:
<hr><i>Copyright (c) 2000<a >Acmee Inc</a>,<br>All Rights Reserved.</i>
當需要copyright時,引入
<#include "/copyright_footer.html">
實例/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/copyright.html
<hr><i> Copyright (c) 2000<a >Acmee Inc</a>, <br> All Rights Reserved.中文測試</i>
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/include.ftl
<h3>include指令</h3> <#include "copyright.html">
測試:http://localhost/test/2/include/null1.4.2.4 聯合使用指令
指令可以嵌套使用;1.4.2.5 處理不存在的變量
<h1>Welcome ${user!"Anonymous"}!</h1>
通過在變量名后邊跟一個!
和默認值。
<h1>Welcome ${user!"Anonymous"}!</h1>
可以使用??詢問freemarker一個變量是否存在,將它和if指令合并,那么如果user變量不存在的話將會忽略整個問候代碼段;
<#if user??><h1>Welcome ${user}!</h1></#if>
對于多級訪問的變量,比如animals.python.price,書寫代碼:animals.python.price!0,僅當animals.python存在且最后一個子變量price可能不存在(這種情況下我們假設價格是0)。如果animals或者python不存在,那么模板處理過程將會以“未定義的變量”錯誤而停止。為了防止這種情況的發生,可以這樣來書寫代碼(animals.python.price)!0
。這種情況下當animals或python不存在時表達式的結果仍然是0。對于??也是同樣用來的處理這種邏輯的,animals.python.price??對比(animals.python.price)??來看;實例
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/null.ftl
<h3>處理不存在的變量</h3> <p>welcome, ${user!"anonymous"}</p> <p>檢測user是否存在,<#if user??>Welcome, ${user}</#if></p> <#--不加括號會報錯: nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing--> <p>多級訪問, ${(animals.python.price)!0}</p>
測試: http://localhost/test/2/null/null
參考資料書
F:/360/Learn/FreeMarker/workspace/FreeMarker-hello-java/
,https://github.com/yejq/FreeMarker-hello-java.git。 F:/360/Learn/freemarker/workspace/FreeMarker-hello-web/
, https://github.com/yejq/FreeMarker-hello-web.git。新聞熱點
疑難解答