FreeMarker笔记 前言第1章 入门
簡介
簡介
FreeMarker是一款模板引擎:一種基于模板的、用來生成輸出文本(任何來自于HTML格式的文本用來自動生成源代碼)的通用工具。它是為Java程序員提供的一個開發(fā)包或者說是類庫。它不是面向最終用戶,而是為程序員提供的可以嵌入他們開發(fā)產(chǎn)品的一款應(yīng)用程序。
特點(diǎn)
功能
基礎(chǔ)
概要、關(guān)鍵字
建議
前言
FreeMarker是一款模板引擎:一種基于模板的、用來生成輸出文本(任何來自于HTML格式的文本用來自動生成源代碼)的通用工具。它是為Java程序員提供的一個開發(fā)包或者說是類庫。它不是面向最終用戶,而是為程序員提供的可以嵌入他們開發(fā)產(chǎn)品的一款應(yīng)用程序。
FreeMarker的設(shè)計(jì)實(shí)際上是被用來生成HTML網(wǎng)頁,尤其是通過基于實(shí)現(xiàn)了MVC(Model View Controller,模型-視圖-控制器)模式的Servlet應(yīng)用程序。使用MVC模式的動態(tài)網(wǎng)頁的構(gòu)思使得你可以將前端設(shè)計(jì)者(編寫HTML)從程序員中分離出來。所有人各司其職,發(fā)揮其擅長的一面。網(wǎng)頁設(shè)計(jì)師可以改寫頁面的顯示效果而不受程序員編譯代碼的影響,因?yàn)閼?yīng)用程序的邏輯(Java程序)和頁面設(shè)計(jì)(FreeMarker模板)已經(jīng)分開了。頁面模板代碼不會受到復(fù)雜的程序代碼影響。這種分離的思想即便對一個程序員和頁面設(shè)計(jì)師是同一個人的項(xiàng)目來說都是非常有用的,因?yàn)榉蛛x使得代碼保持簡潔而且便于維護(hù)。
FreeMarker不是Web應(yīng)用框架。它是Web應(yīng)用框架中的一個適用的組件。
第1章 入門
1.2 模板+數(shù)據(jù)模型=輸出
1.3 數(shù)據(jù)模型一覽
1.4 模板一覽
1.4.1 簡介
FTL tags標(biāo)簽:FreeMarker模板的語言標(biāo)簽。一般以符合#開頭,用戶自定義的FTL標(biāo)簽使用@代替#。
Comments注釋:FreeMarker的注釋和HTML的注釋相似,但是它用<#--和-->來分隔。
directives指令:就是所指的FTL標(biāo)簽。
1.4.2 指令示例
1.4.2.1 if指令
假設(shè)你只想向你的老板Big Joe(而不是其他人)問好,就可以這樣做:
<h1> Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>! </h1>使用<#else>標(biāo)簽:
<#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>實(shí)例
/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><#--大于號兩邊要加括號括起來,否則會以為是結(jié)束標(biāo)簽 --><#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/testIf
1.4.2.2 list指令
當(dāng)需求遍歷集合的內(nèi)容時,list指令是非常好用的。
<#list animals as being><tr><td>${being.name}<td>${being.price} Euros </#list>實(shí)例
/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類型要設(shè)置默認(rèn)輸出值,否則報(bào)錯 --><td>${animal.name}, ${animal.price}, ${animal.protect?c}</td></tr></#list></table>測試:
http://localhost/test/2/list/testList
1.4.2.3 include指令
在當(dāng)前模板中插入其他文件的內(nèi)容。
copyright_footer.html:
<hr> <i> Copyright (c) 2000<a href="http://www.xxx.com">Acmee Inc</a>, <br> All Rights Reserved. </i>當(dāng)需要copyright時,引入
<#include "/copyright_footer.html">實(shí)例
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/copyright.html
<hr> <i>Copyright (c) 2000<a href="http://www.xqsoso.com">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/null
1.4.2.4 聯(lián)合使用指令
指令可以嵌套使用;
1.4.2.5 處理不存在的變量
<h1>Welcome ${user!"Anonymous"}!</h1>通過在變量名后邊跟一個!和默認(rèn)值。
<h1>Welcome ${user!"Anonymous"}!</h1>可以使用??詢問freemarker一個變量是否存在,將它和if指令合并,那么如果user變量不存在的話將會忽略整個問候代碼段;
<#if user??><h1>Welcome ${user}!</h1></#if>對于多級訪問的變量,比如animals.python.price,書寫代碼:animals.python.price!0,僅當(dāng)animals.python存在且最后一個子變量price可能不存在(這種情況下我們假設(shè)價(jià)格是0)。如果animals或者python不存在,那么模板處理過程將會以“未定義的變量”錯誤而停止。為了防止這種情況的發(fā)生,可以這樣來書寫代碼(animals.python.price)!0。這種情況下當(dāng)animals或python不存在時表達(dá)式的結(jié)果仍然是0。對于??也是同樣用來的處理這種邏輯的,animals.python.price??對比(animals.python.price)??來看;
實(shí)例
/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><#--不加括號會報(bào)錯: 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
?
參考資料
書
項(xiàng)目
轉(zhuǎn)載于:https://www.cnblogs.com/yejq/p/3964326.html
總結(jié)
以上是生活随笔為你收集整理的FreeMarker笔记 前言第1章 入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PowerShell3.0入门视频(由J
- 下一篇: netty websocket 简单消息