国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

FreeMarker筆記 前言&第1章 入門

2019-11-14 21:06:42
字體:
供稿:網(wǎng)友
FreeMarker筆記 前言&第1章 入門簡介 簡介

FreeMarker是一款模板引擎:一種基于模板的、用來生成輸出文本(任何來自于HTML格式的文本用來自動(dòng)生成源代碼)的通用工具。它是為java程序員提供的一個(gè)開發(fā)包或者說是類庫。它不是面向最終用戶,而是為程序員提供的可以嵌入他們開發(fā)產(chǎn)品的一款應(yīng)用程序。 特點(diǎn) 功能 基礎(chǔ) 概要、關(guān)鍵字 建議 前言

FreeMarker是一款模板引擎:一種基于模板的、用來生成輸出文本(任何來自于HTML格式的文本用來自動(dòng)生成源代碼)的通用工具。它是為Java程序員提供的一個(gè)開發(fā)包或者說是類庫。它不是面向最終用戶,而是為程序員提供的可以嵌入他們開發(fā)產(chǎn)品的一款應(yīng)用程序。

FreeMarker的設(shè)計(jì)實(shí)際上是被用來生成HTML網(wǎng)頁,尤其是通過基于實(shí)現(xiàn)了MVC(Model View Controller,模型-視圖-控制器)模式的Servlet應(yīng)用程序。使用MVC模式的動(dòng)態(tài)網(wǎng)頁的構(gòu)思使得你可以將前端設(shè)計(jì)者(編寫HTML)從程序員中分離出來。所有人各司其職,發(fā)揮其擅長的一面。網(wǎng)頁設(shè)計(jì)師可以改寫頁面的顯示效果而不受程序員編譯代碼的影響,因?yàn)閼?yīng)用程序的邏輯(Java程序)和頁面設(shè)計(jì)(FreeMarker模板)已經(jīng)分開了。頁面模板代碼不會(huì)受到復(fù)雜的程序代碼影響。這種分離的思想即便對(duì)一個(gè)程序員和頁面設(shè)計(jì)師是同一個(gè)人的項(xiàng)目來說都是非常有用的,因?yàn)榉蛛x使得代碼保持簡潔而且便于維護(hù)。

2014-09-03_101642[5]

FreeMarker不是Web應(yīng)用框架。它是Web應(yīng)用框架中的一個(gè)適用的組件。 第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>        <#--大于號(hào)兩邊要加括號(hào)括起來,否則會(huì)以為是結(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/testIf1.4.2.2 list指令

當(dāng)需求遍歷集合的內(nèi)容時(shí),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)錯(cuò) -->                <td>${animal.name}, ${animal.price}, ${animal.protect?c}</td>            </tr>        </#list>    </table>

測試:

http://localhost/test/2/list/testList1.4.2.3 include指令

在當(dāng)前模板中插入其他文件的內(nèi)容。

copyright_footer.html:

<hr><i>Copyright (c) 2000<a >Acmee Inc</a>,<br>All Rights Reserved.</i>

當(dāng)需要copyright時(shí),引入

<#include "/copyright_footer.html">
實(shí)例

/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 聯(lián)合使用指令

指令可以嵌套使用;1.4.2.5 處理不存在的變量

<h1>Welcome ${user!"Anonymous"}!</h1>

通過在變量名后邊跟一個(gè)!和默認(rèn)值。

<h1>Welcome ${user!"Anonymous"}!</h1>

可以使用??詢問freemarker一個(gè)變量是否存在,將它和if指令合并,那么如果user變量不存在的話將會(huì)忽略整個(gè)問候代碼段;

<#if user??><h1>Welcome ${user}!</h1></#if>

對(duì)于多級(jí)訪問的變量,比如animals.python.price,書寫代碼:animals.python.price!0,僅當(dāng)animals.python存在且最后一個(gè)子變量price可能不存在(這種情況下我們假設(shè)價(jià)格是0)。如果animals或者python不存在,那么模板處理過程將會(huì)以“未定義的變量”錯(cuò)誤而停止。為了防止這種情況的發(fā)生,可以這樣來書寫代碼(animals.python.price)!0。這種情況下當(dāng)animals或python不存在時(shí)表達(dá)式的結(jié)果仍然是0。對(duì)于??也是同樣用來的處理這種邏輯的,animals.python.price??對(duì)比(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>    <#--不加括號(hào)會(huì)報(bào)錯(cuò): nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing-->    <p>多級(jí)訪問, ${(animals.python.price)!0}</p>

測試: http://localhost/test/2/null/null

參考資料書

  1. B1 :《FreeMarker中文版文檔.pdf》
  2. B2 :
項(xiàng)目
  1. P1:F:/360/Learn/FreeMarker/workspace/FreeMarker-hello-java/,https://github.com/yejq/FreeMarker-hello-java.git。
  2. P2:F:/360/Learn/freemarker/workspace/FreeMarker-hello-web/, https://github.com/yejq/FreeMarker-hello-web.git。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新田县| 华蓥市| 松原市| 贵定县| 大荔县| 普格县| 江西省| 保山市| 乐安县| 丽江市| 百色市| 津南区| 平原县| 射阳县| 静安区| 四平市| 屯留县| 泰州市| 博湖县| 神木县| 重庆市| 嘉定区| 江川县| 台东县| 西峡县| 天峻县| 清水河县| 琼结县| 定西市| 珲春市| 丰城市| 怀集县| 成都市| 洱源县| 鹤壁市| 乾安县| 沁阳市| 宁都县| 天镇县| 高青县| 宁乡县|