迷失北京

BlogJava 联系 聚合 管理
  60 Posts :: 0 Stories :: 13 Comments :: 0 Trackbacks

根据网上自己做了测试和一点修改,测试代码还有测试的截图在最下面的部分贴出来了,方便大家参考和学习!不过文章中的关于测试的代码没有作修改,可以直接参考我的!!今天下载了一份freemarker的文档,才发现昨天参考的那篇资料恰恰就是文档,呼呼....啥也不说了!仔细的看了一遍文档,又做了一些实验,代码补充到上来!!

常用语法
一个对象User(就是javabean对象)
1.输出 ${User.name}

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

${User.name?default(‘xxx’)}//默认值xxx
${ User.name!"xxx"}//默认值xxx

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

数字格式:${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.循环读取
<#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 基本用法
宏是和某个变量关联的模板片断,以便在模板中通过用户定义指令使用该变量,下面是一个例子:

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

调用宏时,与使用FreeMarker的其他指令类似,
只是使用@替代FTL标记中的#

<@greet></@greet>

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


macro指令中可以在宏变量之后定义参数,如:

<#macro greet person>
<font size="+2">Hello ${person}!</font>
</#macro>
可以这样使用这个宏变量:
   <@greet person="Fred"/>
但是下面的代码具有不同的意思:
   <@greet person=Fred/>
这意味着将Fred变量的值传给person参数,该值不仅是字符串,还可以是其它类型,甚至是复杂的表达式。


宏可以有多参数,下面是一个例子:
<#macro greet person color>
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro>

可以这样使用该宏变量,其中参数的次序是无关的:
<@greet person="Fred" color="black"/>

可以在定义参数时指定缺省值,否则,在调用宏的时候,必须对所有参数赋值:
<#macro greet person color="black">
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro>

注意:宏的参数是局部变量,只能在宏定义中有效。


在宏里嵌套内容
FreeMarker的宏可以有嵌套内容,
<#nested>指令会执行宏调用指令开始和结束标记之间的模板片断,举一个简单的例子:

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

执行宏调用:
<@border>The bordered text</@border>
输出结果:
<table border=4 cellspacing=0 cellpadding=4>
<tr><td>
       The bordered text
</tr></td>
</table>

<#nested>指令可以被多次调用,每次都会执行相同的内容。
<#macro do_thrice>
<#nested>
<#nested>
<#nested>
</#macro>


<@do_thrice>
Anything.
</@do_thrice>

FMPP 输出结果:
Anything.
Anything.
Anything.


嵌套内容可以是有效的FTL,下面是一个有些复杂的例子,我们将上面三个宏组合起来:
<@border>
<ul>
<@do_thrice>
    <li><@greet person="Joe"/>
</@do_thrice>
</ul>
</@border>
输出结果:
<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>

宏定义中的局部变量对嵌套内容是不可见的,例如:
<#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>

输出结果:
test 3/1: ? ? ?
test 3/2: ? ? ?
test 3/3: ? ? ?


在宏定义中使用循环变量
nestted指令也可以有循环变量(循环变量的含义见下节),调用宏的时候在宏指令的参数后面依次列出循环变量的名字,格式如下:

<@ 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是宏的参数,c, halfc,last则为循环变量,输出结果:
1. 0.5
2. 1
3. 1.5
4. 2 Last!
循环变量和宏标记指定的不同不会有问题,如果调用时少指定了循环变量,那么多余的值不可见。调用时多指定了循环变量,多余的循环变量

不会被创建:
<@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指令创建和替换。
局部变量:在宏定义体中有效,使用local指令创建和替换。
循环变量:只能存在于指令的嵌套内容,由指令(如list)自动创建;宏的参数是局部变量,而不是循环变量
局部变量隐藏(而不是覆盖)同名的plain变量;循环变量隐藏同名的局部变量和plain变量,下面是一个例子:

<#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>
输出结果:
1. plain
2. plain
3. local
4. loop
5. local
6. plain
7. loop
8. loop
9. plain2
内部循环变量隐藏同名的外部循环变量,如:
<#list ["loop 1"] as x>
${x}
<#list ["loop 2"] as x>
    ${x}
    <#list ["loop 3"] as x>
      ${x}
    </#list>
    ${x}
</#list>
${x}
</#list>
输出结果:
loop 1
    loop 2
      loop 3
    loop 2
loop 1
模板中的变量会隐藏(而不是覆盖)数据模型中同名变量,如果需要访问数据模型中的同名变量,使用特殊变量global,下面的例子假设数据

模型中的user的值是Big Joe
<#assign user = "Joe Hider">
${user}          <#-- prints: Joe Hider -->
${.globals.user} <#-- prints: Big Joe -->
名字空间
通常情况,只使用一个名字空间,称为主名字空间,但为了创建可重用的宏、变换器或其它变量的集合(通常称库),必须使用多名字空间,

其目的是防止同名冲突
创建库
下面是一个创建库的例子(假设保存在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指令导入库到模板中,Freemarker会为导入的库创建新的名字空间,并可以通过import指令中指定的散列变量访问库中的变量:
<#import "/lib/my_test.ftl" as my>
<#assign mail="fred@acme.com">
<@my.copyright date="1999-2002"/>
${my.mail}
${mail}
输出结果:
<p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
<br>Email: jsmith@acme.com</p>
jsmith@acme.com
fred@acme.com
可以看到例子中使用的两个同名变量并没有冲突,因为它们位于不同的名字空间。还可以使用assign指令在导入的名字空间中创建或替代变量

,下面是一个例子:
<#import "/lib/my_test.ftl" as my>
${my.mail}
<#assign mail="jsmith@other.com" in my>
${my.mail}
输出结果:
jsmith@acme.com
jsmith@other.com
数据模型中的变量任何地方都可见,也包括不同的名字空间,下面是修改的库:
<#macro copyright date>
<p>Copyright (C) ${date} ${user}. All rights reserved.</p>
</#macro>
<#assign mail = "${user}@acme.com">
假设数据模型中的user变量的值是Fred,则下面的代码:
<#import "/lib/my_test.ftl" as my>
<@my.copyright date="1999-2002"/>
${my.mail}
输出结果:
<p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>Fred@acme.com

 

我的测测实例和测试结果截图:

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:在宏里嵌套内容#nested指令.啥意思呢:好比是jsp的include指令!
			<#macro border>
				<table border=4 cellspacing=0 cellpadding=4>
					<tr>
						<td>
				    	<#nested>
						</td>
					</tr>
				</table>
			</#macro>
			<#--引用-->
			<@border>The bordered text</@border>
			
			<#--循环引用-->
			<#macro do_thrice>
				<ul>
					<li><#nested></li>
					<li><#nested></li>
					<li><#nested></li>
				</ul>
			</#macro>
			<@do_thrice>
				An cycle String
			</@do_thrice>
			
			5: 将上面所有的宏联合显示
			<#macro do_thrice>
				<@border>
					<ul>
						<li><#nested></li>
						<li><#nested></li>
						<li><#nested></li>
					</ul>
				</@border>	
			</#macro>
			<@do_thrice>
				Content
			</@do_thrice>
			
			6:在宏中定义使用循环变量<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: 内部循环变量隐藏同名的外部循环变量
			<#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:需要访问数据模型中的同名变量,使用特殊变量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指令在导入的名字空间中创建或替代变量
			<#import "taglib.ftl" as my>
			${my.mail}<br />
			<#assign mail="fred@acme.com" in my>
			${my.mail}
			-->
			3: 数据模型中的变量任何地方都可见,也包括不同的名字空间
			<#import "taglib1.ftl" as my>
			<@my.copyright date="1999-2002"/>
		</div>
		
		<#-- ******************************我看手册做的一些例子************************* -->
		
		<#-- 关于字符串的一些操作 -->
		<div>
			(1)<#--Interpolation向文本中插入表达式的值-->
			<#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)<#--散列操作: 连接符号":"-->
			<#assign ages={"lei":30, "hou":20}+{"ying":19, "hou": 10} />
				---lei:${ages.lei}<br />
				---hou:${ages.hou}<br />
				---ying:${ages.ying}<br />
		</div>
		
		<#-- 关于运算的操作  -->
		<div>
			<#assign x=5 />
			(1)<#-- 使用内建的操作后的整数部分,去掉小数点以后的部分,直接取整 -->
			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的比较=,!=,>,>=,<,<=, 还有关于逻辑操作符:"&&", "||", "!" --><br />
			(3)<#-- 关于字符串使用的:html对html进行编码;cap_first字符串首字母大写;lower_case字符串小写;upper_case大写;trim去掉首尾空格 -->
				<#--序列使用的:size获得序列中的元素个数-->
				<#--数字是用的:int-->
				<#assign names=["a<ht&ml>", "b", "c"] />
				${names[0]?upper_case?html}
			<br />	
			(4)关于interpolation的详细使用
			<#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)<#--关于时间的设置-->			
			${dd?string("yy-MM-dd, HH:mm:ss")}
			
			<#assign a=true/>
			${a?string("yes", "no")}
			<br/>
			(6)<#-- 关于数字的格式化(数字的Interpolation:#{exp; format}):mX:小数部分最小x位,MX:小数部分最大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">

  

测试结果:

posted on 2011-04-15 22:07 王康 阅读(410) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: