3,輸出Map元素 這裏的Map對象能夠是直接HashMap的實例,甚至包括JavaBean實例,對於JavaBean實例而言,咱們同樣能夠把其當成屬性爲key,屬性值爲value的Map實例.爲了輸出Map元素的值,可使用點語法或方括號語法.假若有下面的數據模型: Map root = new HashMap(); Book book = new Book(); Author author = new Author(); author.setName("annlee"); author.setAddress("gz"); book.setName("struts2"); book.setAuthor(author); root.put("info","struts"); root.put("book", book);
看以下的例子: <#noparse> <#list books as book> <tr><td>${book.name}<td>做者:${book.author} </#list> </#noparse> 輸出以下: <#list books as book> <tr><td>${book.name}<td>做者:${book.author} </#list>
4.7 escape , noescape指令
escape指令致使body區的插值都會被自動加上escape表達式,但不會影響字符串內的插值,只會影響到body內出現的插值,使用escape指令的語法格式以下: <#escape identifier as expression>... <#noescape>...</#noescape> </#escape>
看以下的代碼: <#escape x as x?html> First name:${firstName} Last name:${lastName} Maiden name:${maidenName} </#escape> 上面的代碼等同於: First name:${firstName?html} Last name:${lastName?html} Maiden name:${maidenName?html}
escape指令在解析模板時起做用而不是在運行時起做用,除此以外,escape指令也嵌套使用,子escape繼承父escape的規則,以下例子: <#escape x as x?html> Customer Name:${customerName} Items to ship; <#escape x as itemCodeToNameMap[x]> ${itemCode1} ${itemCode2} ${itemCode3} ${itemCode4} </#escape> </#escape> 上面的代碼相似於: Customer Name:${customerName?html} Items to ship; ${itemCodeToNameMap[itemCode1]?html} ${itemCodeToNameMap[itemCode2]?html} ${itemCodeToNameMap[itemCode3]?html} ${itemCodeToNameMap[itemCode4]?html}
用例 字符串 <#switch being.size> <#case "small"> This will be processed if it is small <#break> <#case "medium"> This will be processed if it is medium <#break> <#case "large"> This will be processed if it is large <#break> <#default> This will be processed if it is neither </#switch> 數字 <#switch x> <#case x = 1> 1 <#case x = 2> 2 <#default> d </#switch>
include 語法 <#include filename> or <#include filename options> options包含兩個屬性 encoding=」GBK」 編碼格式 parse=true 是否做爲ftl語法解析,默認是true,false就是以文本方式引入.注意在ftl文件里布爾值都是直接賦值的如parse=true,而不是parse=」true」 用例 /common/copyright.ftl包含內容 Copyright 2001-2002 ${me}<br> All rights reserved. 模板文件 <#assign me = "Juila Smith"> <h1>Some test</h1> <p>Yeah. <hr> <#include "/common/copyright.ftl" encoding=」GBK」> 輸出結果 <h1>Some test</h1> <p>Yeah. <hr> Copyright 2001-2002 Juila Smith All rights reserved.
Import 語法 <#import path as hash> 相似於java裏的import,它導入文件,而後就能夠在當前文件裏使用被導入文件裏的宏組件
用例
假設mylib.ftl裏定義了宏copyright那麼咱們在其餘模板頁面裏能夠這樣使用 <#import "/libs/mylib.ftl" as my>
<@my.copyright date="1999-2002"/>
"my"在freemarker裏被稱做namespace
compress 語法 <#compress> ... </#compress> 用來壓縮空白空間和空白的行 用例 <#assign x = " moo \n\n "> (<#compress> 1 2 3 4 5 ${moo} test only
I said, test only
</#compress>) 輸出 (1 2 3 4 5 moo test only I said, test only) escape, noescape 語法 <#escape identifier as expression> ... <#noescape>...</#noescape> ... </#escape> 用例 主要使用在類似的字符串變量輸出,好比某一個模塊的全部字符串輸出都必須是html安全的,這個時候就可使用該表達式 <#escape x as x?html> First name: ${firstName} <#noescape>Last name: ${lastName}</#noescape> Maiden name: ${maidenName} </#escape> 相同表達式 First name: ${firstName?html} Last name: ${lastName } Maiden name: ${maidenName?html} assign 語法 <#assign name=value> or <#assign name1=value1 name2=value2 ... nameN=valueN> or <#assign same as above... in namespacehash> or <#assign name> capture this </#assign> or <#assign name in namespacehash> capture this </#assign> 用例 生成變量,而且給變量賦值 給seasons賦予序列值 <#assign seasons = ["winter", "spring", "summer", "autumn"]>
給變量test加1 <#assign test = test + 1>
給my namespage 賦予一個變量bgColor,下面能夠經過my.bgColor來訪問這個變量 <#import "/mylib.ftl" as my> <#assign bgColor="red" in my>
將一段輸出的文本做爲變量保存在x裏 下面的陰影部分輸出的文本將被賦值給x <#assign x> <#list 1..3 as n> ${n} <@myMacro /> </#list> </#assign> Number of words: ${x?word_list?size} ${x}
<#macro name param1 param2 ... paramN> ... <#nested loopvar1, loopvar2, ..., loopvarN> ... <#return> ... </#macro> 用例 <#macro test foo bar="Bar" baaz=-1> Test text, and the params: ${foo}, ${bar}, ${baaz} </#macro> <@test foo="a" bar="b" baaz=5*5-2/> <@test foo="a" bar="b"/> <@test foo="a" baaz=5*5-2/> <@test foo="a"/> 輸出 Test text, and the params: a, b, 23 Test text, and the params: a, b, -1 Test text, and the params: a, Bar, 23 Test text, and the params: a, Bar, -1 定義循環輸出的宏 <#macro list title items> <p>${title?cap_first}: <ul> <#list items as x> <li>${x?cap_first} </#list> </ul> </#macro> <@list items=["mouse", "elephant", "python"] title="Animals"/> 輸出結果 <p>Animals: <ul> <li>Mouse <li>Elephant <li>Python </ul> 包含body的宏 <#macro repeat count> <#list 1..count as x> <#nested x, x/2, x==count> </#list> </#macro> <@repeat count=4 ; c halfc last> ${c}. ${halfc}<#if last> Last!</#if> </@repeat> 輸出 1. 0.5 2. 1 3. 1.5 4. 2 Last!
判斷對象是否是null <#if mouse?exists> Mouse found <#else> 也能夠直接${mouse?if_exists})輸出布爾形 經常使用格式化日期 openingTime必須是Date型,詳細查看freemarker文檔 Reference->build-in referece->build-in for date
下面兩個可能在代碼生成的時候使用(在引號前加」\」) j_string: 在字符串引號前加」\」 <#assign beanName = 'The "foo" bean.'> String BEAN_NAME = "${beanName?j_string}"; 打印輸出: String BEAN_NAME = "The \"foo\" bean."; js_string: <#assign user = "Big Joe's \"right hand\"."> <script> alert("Welcome ${user}!"); </script> 打印輸出 alert("Welcome Big Joe\'s \"right hand\"!");
替換字符串 replace ${s?replace(‘ba’, ‘XY’ )} ${s?replace(‘ba’, ‘XY’ , ‘規則參數’)}將s裏的全部的ba替換成xy 規則參數包含: i r m s c f 具體含義以下: · i: 大小寫不區分. · f: 只替換第一個出現被替換字符串的字符串 · r: XY是正則表達式 · m: Multi-line mode for regular expressions. In multi-line mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the string. By default these expressions only match at the beginning and the end of the entire string. · s: Enables dotall mode for regular expressions (same as Perl singe-line mode). In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators. · c: Permits whitespace and comments in regular expressions.
與webwork整合以後 經過配置的servlet 已經把request,session等對象置入了數據模型中 在view中存在下面的對象 咱們能夠在ftl中${req}來打印req對象 · req - the current HttpServletRequest · res - the current HttpServletResponse · stack - the current OgnlValueStack · ognl - the OgnlTool instance · webwork - an instance of FreemarkerWebWorkUtil · action - the current WebWork action · exception - optional the Exception instance, if the view is a JSP exception or Servlet exception view view中值的搜索順序 ${name}將會如下面的順序查找name值 · freemarker variables · value stack · request attributes · session attributes · servlet context attributes 在模板裏ftl裏使用標籤 注意,若是標籤的屬性值是數字,那麼必須採用nubmer=123方式給屬性賦值 JSP頁面 <%@page contentType="text/html;charset=ISO-8859-2" language="java"%> <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>