FreeMaker的特性 : 訥訥夠生成各種,HTML-XML-RTF-Java源代碼等
容易嵌套到產(chǎn)品中 : 輕量級(jí)的,不需要Servlet環(huán)境
常用的命令 : include . if/elseif/else . 循環(huán)
.ftl是freemaker 默認(rèn)的文件后綴
FreeMarker jar包? ?:? ? ? ?<dependency>
????????????????????????????????????????????????????????<groupId>org.freemarker</groupId>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????? <artifactId>freemarker</artifactId>
????????????????????????????????????????????????????????<version>2.3.30</version>
????????????????????????????????????????????</dependency>
????????????????????????????????????????????<dependency>
????????????????????????????????????????????????????????<groupId>javax.servlet</groupId>
????????????????????????????????????????????????????????<artifactId>javax.servlet-api</artifactId>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<version>4.0.1</version>
????????????????????????????????????????????????????????<scope>provided</scope>
????????????????????????????????????????????????</dependency>
FreeMarker? ? ? ? ? ?插件地址? ?
? ? ? ? ? ? ? ?Tomcat? ?官方? ? ? ? http://tomcat.apache.org/maven-plugin-2.2/
? ? ? ? ? ? ? Jetty? ? ? ? ?官方? ? ? ? https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
注釋 :? ?1. 在FreeMarker中,html中的標(biāo)簽均可適用
? ? ? ? ? ? ? 2. js.css均可適用,語法一致
FreeMarker? 數(shù)據(jù)類型
? ? ? ? ? ? ? ? ? ? ? 布爾類型 :? 在freemarker在頁面中不能直接輸出,如果想要輸出必須轉(zhuǎn)化成字符串
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 方式一 :? $(flag?c)? 在后面 + ?c? 設(shè)置什么輸出什么
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 方式二 : $(flag?string("yes","no"))? ? 在后面 + ?string? ?設(shè)置ture返回前面的,設(shè)置false返回
????????????????????????????????????????????????????????????????????????????????????????????????????????????后面的
????????????????????????日期類型 :? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?方式一 :?${dateddd?date}? ? ? ? ? 年月日
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?方式二 :?${dateddd?time}? ? ? ? ? 時(shí)分秒
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?方式三 :?${dateddd?datetime}? ? 年月日時(shí)分秒
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 方式四 :?${dateddd?string("yyyy/MM/dd HH:mm:ss")}? 年月日時(shí)分秒 ------? 可以自己
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 設(shè)置格式??
assign 定義變量? ? ? ? ? ? ? ? ? ? ?定義單個(gè)變量 -- <#assign name="你好呀!">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 頁面獲取變量 -- ${name}
? ?????????????????????????????????????????????????定義多個(gè)變量 -- <#assign num=1,names = ["張三","王五","李四"]>
????????????????????????????????????????????????????頁面火氣多個(gè)變量 -- ${num}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ${names?join(',')}? ? ? ? ?用逗號(hào)將多個(gè)內(nèi)容分開
list? 遍歷? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?形式 1:
????????????????????????????????????????????????????????????<#list? ? ??sequence? ?as? ? ?item>
? ??????????????????????????????????????????????????????????????????????????Part? repeated? for? each? item
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#else>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Part executed when there are 0 items
? ??????????????????????????????????????????????????????????????</#list>
????????????????????????????????????????????????????這里:
????????????????????????????????????????????????????else?部分是可選的, 而且僅僅從 FreeMarker 2.3.23 版本開始支持。
? ??????????????????????????????????????????????????sequence: 將我們想要迭代的項(xiàng),算作是序列或集合的表達(dá)式
? ??????????????????????????????????????????????????item:?循環(huán)變量?的名稱 (不是表達(dá)式)
????????????????????????????????????????????????????在標(biāo)簽之間的多個(gè) "parts" 可以是任意的FTL (包括嵌套的?list)
? ? ? 自定義指令? ? ?macro? ? ? ? ? ? ? ? ? ? <#macro user>? ? 中間添加指令? (可以嵌套標(biāo)簽
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?可以定義有參數(shù)的指令
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?添加list遍歷 )? ? ? ?</#macro>
????????????????????????????????????????????????????????????通過? ?<@user></@user>? ?在頁面輸出出來
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 可以多次重復(fù)來調(diào)用,使用
? ? ? 占位符? ?nested? ? ? 概述 :?指令執(zhí)行自定義指令開始和結(jié)束標(biāo)簽中間的模板片段。 嵌套的片段可以包含模板
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?中? 任意合法的內(nèi)容:插值,指令等...它在上下文環(huán)境中被執(zhí)行, 也就是宏被調(diào)用的地方
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ,而不是宏定義體的上下文中。因此,比如, 你不能看到嵌套部分的宏的局部變量。如
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?果你沒有調(diào)用
? ? ?import? ?引用指令? ? ? ? 格式 :? <#import? ?path? ?as? ?hash>
? ??????????????????????????????????????path:模板的路徑。 這是一個(gè)算作是字符串的表達(dá)式。(換句話說,它不是一個(gè)固定的
????????????????????????????????????????字 符串, 它可以是這樣的一些東西,比如,profile.baseDir + "/menu.ftl"。)
? ??????????????????????????????????????hash: 訪問命名空間的哈希表變量不帶引號(hào)的名字。不是表達(dá)式。 (如果要引入動(dòng)態(tài)創(chuàng)
????????????????????????????????????????建的名字,那么就不得不使用?這個(gè)技巧。)
? ? include? 包含指令? ? ? ??<#include? ?path>? 或? <#include? ?path? ??options>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 引用其他文件
? ??????????????????????????????????????path: 要包含文件的路徑;一個(gè)算作是字符串的表達(dá)式。(用其他話說, 它不用是一個(gè)
????????????????????????????????????????固定的字符串,它也可以是像?profile.baseDir + "/menu.ftl"這樣的東西。)
? ??????????????????????????????????????options: 一個(gè)或多個(gè)這樣的選項(xiàng):?encoding=encoding,?parse=parse
? ??????????????????????????????????????encoding: 算作是字符串的表達(dá)式
? ??????????????????????????????????????parse: 算作是布爾值的表達(dá)式(為了向下兼容,也接受一部分字符串值)
? ??????????????????????????????????????ignore_missing: 算作是布爾值的表達(dá)式