迷失北京

          BlogJava 聯(lián)系 聚合 管理
            60 Posts :: 0 Stories :: 13 Comments :: 0 Trackbacks

          根據(jù)網(wǎng)上自己做了測(cè)試和一點(diǎn)修改,測(cè)試代碼還有測(cè)試的截圖在最下面的部分貼出來(lái)了,方便大家參考和學(xué)習(xí)!不過文章中的關(guān)于測(cè)試的代碼沒有作修改,可以直接參考我的??!今天下載了一份freemarker的文檔,才發(fā)現(xiàn)昨天參考的那篇資料恰恰就是文檔,呼呼....啥也不說了!仔細(xì)的看了一遍文檔,又做了一些實(shí)驗(yàn),代碼補(bǔ)充到上來(lái)?。?/p>

          常用語(yǔ)法
          一個(gè)對(duì)象User(就是javabean對(duì)象)
          1.輸出 ${User.name}

          空值判斷:${User.name?if_exists },

          ${User.name?default(‘xxx’)}//默認(rèn)值xxx
          ${ User.name!"xxx"}//默認(rèn)值xxx

          日期格式:${User.date?string('yyyy-MM-dd')}

          數(shù)字格式:${User?string.number}--20
          ${User?string.currency}--<#-- $20.00 -->
          ${User?string.percent}—<#-- 20% -->

          插入布爾值:
          <#assign foo=ture />

          2.邏輯判斷
          a:
          <#if condition>...
          <#elseif condition2>...
          <#elseif condition3>......
          <#else>...
          其中空值判斷可以寫成<#if User.name?? >
          </#if>


          b:
          <#switch value>
          <#case refValue1>
              ...
              <#break>
          <#case refValue2>
              ...
              <#break>
          ...
          <#case refValueN>
              ...
              <#break>
          <#default>
              ...
          </#switch>

          3.循環(huán)讀取
          <#list sequence as item>
          ...
          </#list>
          空值判斷<#if UserList?size = 0></#list>


          e.g.
          <#list employees as e>
          ${e_index}. ${e.name}
          </#list>
          輸出:
          1. Readonly
          2. Robbin

          4.FreeMarker


          /模板

          Macro:宏是在模板中使用macro指令定義

          l.1 基本用法
          宏是和某個(gè)變量關(guān)聯(lián)的模板片斷,以便在模板中通過用戶定義指令使用該變量,下面是一個(gè)例子:

          <#macro greet>
          <font size="+2">Hello Joe!</font>
          </#macro>

          調(diào)用宏時(shí),與使用FreeMarker的其他指令類似,
          只是使用@替代FTL標(biāo)記中的#。

          <@greet></@greet>

          <#--<@greet/>-->


          macro指令中可以在宏變量之后定義參數(shù),如:

          <#macro greet person>
          <font size="+2">Hello ${person}!</font>
          </#macro>
          可以這樣使用這個(gè)宏變量:
             <@greet person="Fred"/>
          但是下面的代碼具有不同的意思:
             <@greet person=Fred/>
          這意味著將Fred變量的值傳給person參數(shù),該值不僅是字符串,還可以是其它類型,甚至是復(fù)雜的表達(dá)式。


          宏可以有多參數(shù),下面是一個(gè)例子:
          <#macro greet person color>
          <font size="+2" color="${color}">Hello ${person}!</font>
          </#macro>

          可以這樣使用該宏變量,其中參數(shù)的次序是無(wú)關(guān)的:
          <@greet person="Fred" color="black"/>

          可以在定義參數(shù)時(shí)指定缺省值,否則,在調(diào)用宏的時(shí)候,必須對(duì)所有參數(shù)賦值:
          <#macro greet person color="black">
          <font size="+2" color="${color}">Hello ${person}!</font>
          </#macro>

          注意:宏的參數(shù)是局部變量,只能在宏定義中有效。


          在宏里嵌套內(nèi)容
          FreeMarker的宏可以有嵌套內(nèi)容,
          <#nested>指令會(huì)執(zhí)行宏調(diào)用指令開始和結(jié)束標(biāo)記之間的模板片斷,舉一個(gè)簡(jiǎn)單的例子:

          <#macro border>
          <table border=4 cellspacing=0 cellpadding=4><tr><td>
              <#nested>
          </tr></td></table>
          </#macro>

          執(zhí)行宏調(diào)用:
          <@border>The bordered text</@border>
          輸出結(jié)果:
          <table border=4 cellspacing=0 cellpadding=4>
          <tr><td>
                 The bordered text
          </tr></td>
          </table>

          <#nested>指令可以被多次調(diào)用,每次都會(huì)執(zhí)行相同的內(nèi)容。
          <#macro do_thrice>
          <#nested>
          <#nested>
          <#nested>
          </#macro>


          <@do_thrice>
          Anything.
          </@do_thrice>

          FMPP 輸出結(jié)果:
          Anything.
          Anything.
          Anything.


          嵌套內(nèi)容可以是有效的FTL,下面是一個(gè)有些復(fù)雜的例子,我們將上面三個(gè)宏組合起來(lái):
          <@border>
          <ul>
          <@do_thrice>
              <li><@greet person="Joe"/>
          </@do_thrice>
          </ul>
          </@border>
          輸出結(jié)果:
          <table border=4 cellspacing=0 cellpadding=4><tr><td>
          <ul>
              <li><font size="+2">Hello Joe!</font>
              <li><font size="+2">Hello Joe!</font>
              <li><font size="+2">Hello Joe!</font>
          </ul>
          </tr></td></table>

          宏定義中的局部變量對(duì)嵌套內(nèi)容是不可見的,例如:
          <#macro repeat count>
          <#local y = "test">
          <#list 1..count as x>
              ${y} ${count}/${x}: <#nested>
          </#list>
          </#macro>

          <@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}</@repeat>

          輸出結(jié)果:
          test 3/1: ? ? ?
          test 3/2: ? ? ?
          test 3/3: ? ? ?


          在宏定義中使用循環(huán)變量
          nestted指令也可以有循環(huán)變量(循環(huán)變量的含義見下節(jié)),調(diào)用宏的時(shí)候在宏指令的參數(shù)后面依次列出循環(huán)變量的名字,格式如下:

          <@ macro_name paramter list; loop variable list[,]>

          例如:
          <#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>

          這里count是宏的參數(shù),c, halfc,last則為循環(huán)變量,輸出結(jié)果:
          1. 0.5
          2. 1
          3. 1.5
          4. 2 Last!
          循環(huán)變量和宏標(biāo)記指定的不同不會(huì)有問題,如果調(diào)用時(shí)少指定了循環(huán)變量,那么多余的值不可見。調(diào)用時(shí)多指定了循環(huán)變量,多余的循環(huán)變量

          不會(huì)被創(chuàng)建:
          <@repeat count=4 ; c, halfc, last>
          ${c}. ${halfc}<#if last> Last!</#if>
          </@repeat>

          <@repeat count=4 ; c, halfc>
          ${c}. ${halfc}
          </@repeat>

          <@repeat count=4>
          Just repeat it...
          </@repeat>

          在模板中定義變量
          在模板中定義的變量有三種類型:
          plain變量:可以在模板的任何地方訪問,包括使用include指令插入的模板,使用assign指令創(chuàng)建和替換。
          局部變量:在宏定義體中有效,使用local指令創(chuàng)建和替換。
          循環(huán)變量:只能存在于指令的嵌套內(nèi)容,由指令(如list)自動(dòng)創(chuàng)建;宏的參數(shù)是局部變量,而不是循環(huán)變量
          局部變量隱藏(而不是覆蓋)同名的plain變量;循環(huán)變量隱藏同名的局部變量和plain變量,下面是一個(gè)例子:

          <#assign x = "plain">

          ${x} <#-- we see the plain var. here -->

          <@test/>
          6. ${x} <#-- the value of plain var. was not changed -->
          <#list ["loop"] as x>
          7. ${x} <#-- now the loop var. hides the plain var. -->
              <#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->
          8. ${x} <#-- it still hides the plain var. -->
          </#list>
          9. ${x} <#-- the new value of plain var. -->
          <#macro test>
          2. ${x} <#-- we still see the plain var. here -->
          <#local x = "local">
          3. ${x} <#-- now the local var. hides it -->
          <#list ["loop"] as x>
              4. ${x} <#-- now the loop var. hides the local var. -->
          </#list>
          5. ${x} <#-- now we see the local var. again -->
          </#macro>
          輸出結(jié)果:
          1. plain
          2. plain
          3. local
          4. loop
          5. local
          6. plain
          7. loop
          8. loop
          9. plain2
          內(nèi)部循環(huán)變量隱藏同名的外部循環(huán)變量,如:
          <#list ["loop 1"] as x>
          ${x}
          <#list ["loop 2"] as x>
              ${x}
              <#list ["loop 3"] as x>
                ${x}
              </#list>
              ${x}
          </#list>
          ${x}
          </#list>
          輸出結(jié)果:
          loop 1
              loop 2
                loop 3
              loop 2
          loop 1
          模板中的變量會(huì)隱藏(而不是覆蓋)數(shù)據(jù)模型中同名變量,如果需要訪問數(shù)據(jù)模型中的同名變量,使用特殊變量global,下面的例子假設(shè)數(shù)據(jù)

          模型中的user的值是Big Joe
          <#assign user = "Joe Hider">
          ${user}          <#-- prints: Joe Hider -->
          ${.globals.user} <#-- prints: Big Joe -->
          名字空間
          通常情況,只使用一個(gè)名字空間,稱為主名字空間,但為了創(chuàng)建可重用的宏、變換器或其它變量的集合(通常稱庫(kù)),必須使用多名字空間,

          其目的是防止同名沖突
          創(chuàng)建庫(kù)
          下面是一個(gè)創(chuàng)建庫(kù)的例子(假設(shè)保存在lib/my_test.ftl中):
          <#macro copyright date>
          <p>Copyright (C) ${date} Julia Smith. All rights reserved.
          <br>Email: ${mail}</p>
          </#macro> 
          <#assign mail = "jsmith@acme.com">
          使用import指令導(dǎo)入庫(kù)到模板中,Freemarker會(huì)為導(dǎo)入的庫(kù)創(chuàng)建新的名字空間,并可以通過import指令中指定的散列變量訪問庫(kù)中的變量:
          <#import "/lib/my_test.ftl" as my>
          <#assign mail="fred@acme.com">
          <@my.copyright date="1999-2002"/>
          ${my.mail}
          ${mail}
          輸出結(jié)果:
          <p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
          <br>Email: jsmith@acme.com</p>
          jsmith@acme.com
          fred@acme.com
          可以看到例子中使用的兩個(gè)同名變量并沒有沖突,因?yàn)樗鼈兾挥诓煌拿挚臻g。還可以使用assign指令在導(dǎo)入的名字空間中創(chuàng)建或替代變量

          ,下面是一個(gè)例子:
          <#import "/lib/my_test.ftl" as my>
          ${my.mail}
          <#assign mail="jsmith@other.com" in my>
          ${my.mail}
          輸出結(jié)果:
          jsmith@acme.com
          jsmith@other.com
          數(shù)據(jù)模型中的變量任何地方都可見,也包括不同的名字空間,下面是修改的庫(kù):
          <#macro copyright date>
          <p>Copyright (C) ${date} ${user}. All rights reserved.</p>
          </#macro>
          <#assign mail = "${user}@acme.com">
          假設(shè)數(shù)據(jù)模型中的user變量的值是Fred,則下面的代碼:
          <#import "/lib/my_test.ftl" as my>
          <@my.copyright date="1999-2002"/>
          ${my.mail}
          輸出結(jié)果:
          <p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>Fred@acme.com

           

          我的測(cè)測(cè)實(shí)例和測(cè)試結(jié)果截圖:

          helllo.flt

          <html>
          	<head>
          		<title>hello!</title>
          	</head>
          	<body>
          		<div>list:
          			<#list user?sort_by("id") as u>
          				<h1>${u.id}----${u.name}-----${u.date?string('yy-MM-dd HH:mm:ss')}</h1>
          			</#list>
          		</div>
          		<div>
          			insert a boolean:
          			<#assign foo=true />
          			${foo?string}
          		</div>
          		
          		<div>
          			if,elseif, else
          			<#assign a=2 />
          			<#if a=1>
          				a value:1
          			<#elseif a=2>
          				a value:2
          			<#elseif a=3>
          				a value:3	
          			<#else>
          				a value is else
          			</#if>				
          		</div>
          		
          		<div>
          			case:
          			<#assign str="wangkang">
          			
          			<#switch str>
          				<#case "wangkang1">
          					${str}
          				<#break>
          				<#case "wangkang2">
          					${str}
          				<#break>
          				<#case "wangkang">
          					${str}
          				<#break>
          			</#switch>	
          		</div>
          		
          		<div>
          			1:common
          			<#macro greet1>
          				<font size="+2" color="red">Hello Kitty!</font>
          			</#macro>
          			<@greet1>macro:</@greet1><br />
          			
          			2:design a parameter
          			<#macro greet2 person>
          				<font size="+2" color="red">hello ${person}</font>
          			</#macro>
          			<@greet2 person="wangkang!"/><br />
          			
          			3:design two parameters
          			<#macro greet3 person color="blue">
          				<font size="+2" color="${color}">hello ${person}</font>
          			</#macro>
          			<@greet3 person="wangkang!" color="green"/><br />
          			
          			4:在宏里嵌套內(nèi)容#nested指令.啥意思呢:好比是jsp的include指令!
          			<#macro border>
          				<table border=4 cellspacing=0 cellpadding=4>
          					<tr>
          						<td>
          				    	<#nested>
          						</td>
          					</tr>
          				</table>
          			</#macro>
          			<#--引用-->
          			<@border>The bordered text</@border>
          			
          			<#--循環(huán)引用-->
          			<#macro do_thrice>
          				<ul>
          					<li><#nested></li>
          					<li><#nested></li>
          					<li><#nested></li>
          				</ul>
          			</#macro>
          			<@do_thrice>
          				An cycle String
          			</@do_thrice>
          			
          			5: 將上面所有的宏聯(lián)合顯示
          			<#macro do_thrice>
          				<@border>
          					<ul>
          						<li><#nested></li>
          						<li><#nested></li>
          						<li><#nested></li>
          					</ul>
          				</@border>	
          			</#macro>
          			<@do_thrice>
          				Content
          			</@do_thrice>
          			
          			6:在宏中定義使用循環(huán)變量<br />
          			<#macro repeat count>
          				<#list 1..count as x>
          				    <#nested x, x/2, x==count>
          				    <#--${x?number}-->
          				</#list>
          			</#macro>
          				
          			<@repeat count=4 ; c, halfc, last>
          				${c}. ${halfc}<br />
          				<#if last> Last!</#if>
          			</@repeat>
          			<br />
          			<@repeat count=4 ; c, halfc, last>
          				${c}. ${halfc}<#if last> Last!</#if>
          			</@repeat>
          			<br />
          			<@repeat count=4 ; c, halfc>
          				${c}. ${halfc}
          			</@repeat>
          			<br />
          			<@repeat count=4>
          				Just repeat it...
          			</@repeat>
          			<br />
          		</div>
          		
          		6: freemarker中的變量作用范圍
          		<div>
          			1:<br />
          			<#assign x = "plain">
          			${x} <#-- we see the plain var. here -->			
          			<@test/>
          			6. ${x} <#-- the value of plain var. was not changed -->
          				<#list ["loop"] as x>
          			7. ${x} <#-- now the loop var. hides the plain var. -->
          			    <#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->
          			8. ${x} <#-- it still hides the plain var. -->
          				</#list>
          			9. ${x} <#-- the new value of plain var. -->
          				<#macro test>
          			2. ${x} <#-- we still see the plain var. here -->
          				<#local x = "local">
          			3. ${x} <#-- now the local var. hides it -->
          				<#list ["loop"] as x>
          		    4. ${x} <#-- now the loop var. hides the local var. -->
          				</#list>
          			5. ${x} <#-- now we see the local var. again -->
          				</#macro>
          			<br />
          			2: 內(nèi)部循環(huán)變量隱藏同名的外部循環(huán)變量
          			<#list ["loop 1"] as x>
          				${x}
          					<#list ["loop 2"] as x>
          					    ${x}
          					    <#list ["loop 3"] as x>
          					      ${x}
          					    </#list>
          					    ${x}
          					</#list>
          				${x}
          			</#list>
          			<br />
          			3:需要訪問數(shù)據(jù)模型中的同名變量,使用特殊變量global
          			<#assign num=1000>
          			剛剛定義的num:${num}<br>       
          			模型中的:${.globals.num} 
          		</div>
          		
          		8:名字空間
          		<div>
          			<#--
          			1: 
          			<#import "taglib.ftl" as my>
          			<#assign mail="fred@acme.com">
          			<@my.copyright date="1999-2002"/>
          			${my.mail}
          			${mail}
          			-->
          			<#--
          			2: 用assign指令在導(dǎo)入的名字空間中創(chuàng)建或替代變量
          			<#import "taglib.ftl" as my>
          			${my.mail}<br />
          			<#assign mail="fred@acme.com" in my>
          			${my.mail}
          			-->
          			3: 數(shù)據(jù)模型中的變量任何地方都可見,也包括不同的名字空間
          			<#import "taglib1.ftl" as my>
          			<@my.copyright date="1999-2002"/>
          		</div>
          		
          		<#-- ******************************我看手冊(cè)做的一些例子************************* -->
          		
          		<#-- 關(guān)于字符串的一些操作 -->
          		<div>
          			(1)<#--Interpolation向文本中插入表達(dá)式的值-->
          			<#assign user="Kitty" />
          			${"Hello ${user}"}<br />
          			${"${user}${user}${user}${user}"} <br /><br />
          			${"Hello" + user + "!!"}<br />
          			${user + "\n" +user}<br />
          			
          			(2)<#--list--->
          			${user[0]}${user[4]}<br />
          			${user[0..4]}<br />
          			
          			(3)<#--使用字符串的+-->
          			<#list ["aaa", "bbb"] + ["ccc", "ddd"] as u>
          				${u}
          			</#list><br />
          			
          			(4)<#--散列操作: 連接符號(hào)":"-->
          			<#assign ages={"lei":30, "hou":20}+{"ying":19, "hou": 10} />
          				---lei:${ages.lei}<br />
          				---hou:${ages.hou}<br />
          				---ying:${ages.ying}<br />
          		</div>
          		
          		<#-- 關(guān)于運(yùn)算的操作  -->
          		<div>
          			<#assign x=5 />
          			(1)<#-- 使用內(nèi)建的操作后的整數(shù)部分,去掉小數(shù)點(diǎn)以后的部分,直接取整 -->
          			5/2取整:${(x/2)?int} <br />
          			1.3取整:${1.3?int}<br />
          			1.9取整:${1.89?int}<br />
          			-1.3取整:${1.2?int}<br />
          			-1.9取整:${1.9?int}<br />
          			
          			(2)<#--freemarker的比較=,!=,>,>=,<,<=, 還有關(guān)于邏輯操作符:"&&", "||", "!" --><br />
          			(3)<#-- 關(guān)于字符串使用的:html對(duì)html進(jìn)行編碼;cap_first字符串首字母大寫;lower_case字符串小寫;upper_case大寫;trim去掉首尾空格 -->
          				<#--序列使用的:size獲得序列中的元素個(gè)數(shù)-->
          				<#--數(shù)字是用的:int-->
          				<#assign names=["a<ht&ml>", "b", "c"] />
          				${names[0]?upper_case?html}
          			<br />	
          			(4)關(guān)于interpolation的詳細(xì)使用
          			<#setting number_format="currency"/>
          			<#assign ans=100/>
          			${ans}<br/>
          			${ans?string}<br/>
          			${ans?string.number}<br/>
          			${ans?string.currency}<br/>
          			${ans?string.percent}<br/>
          			<br>
          			(5)<#--關(guān)于時(shí)間的設(shè)置-->			
          			${dd?string("yy-MM-dd, HH:mm:ss")}
          			
          			<#assign a=true/>
          			${a?string("yes", "no")}
          			<br/>
          			(6)<#-- 關(guān)于數(shù)字的格式化(數(shù)字的Interpolation:#{exp; format}):mX:小數(shù)部分最小x位,MX:小數(shù)部分最大x位 -->
          			<#assign ax=2.5890 />
          			<#assign bx=10 />
          			#{ax; m2}<br/>
          			#{bx; M2}<br/>
          			#{ax; m2}<br/>
          			#{bx; M2}<br/>
          			<br/>
          			
          		</div>
          	</body>
          </html>
          

             

          taglib.flt

          <#macro copyright date>
          	<p>Copyright (C) ${date} Julia Smith. All rights reserved.
          	<br>Email: ${mail}</p>
          </#macro> 
          <#assign mail = "jsmith@acme.com">
          

            

          tablib1.flt

          <#macro copyright date>
          	<p>Copyright (C) ${date} Julia Smith. All rights reserved numbers:${num}.
          	<br>Email: ${mail}</p>
          </#macro> 
          <#assign mail = "jsmith@acme.com">
          

            

          測(cè)試結(jié)果:

          posted on 2011-04-15 22:07 王康 閱讀(420) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 泗阳县| 拉萨市| 亚东县| 集贤县| 洮南市| 英超| 元阳县| 石狮市| 琼中| 会昌县| 惠东县| 峨眉山市| 清苑县| 扬州市| 汉寿县| 新津县| 城口县| 大新县| 云安县| 五莲县| 南乐县| 寿光市| 苏州市| 宽城| 元阳县| 新野县| 敦化市| 乌什县| 溆浦县| 长治县| 峨眉山市| 九江县| 开原市| 揭西县| 河曲县| 胶南市| 怀集县| 无极县| 乌兰察布市| 晴隆县| 武山县|