EG.一个对象BOOK
页面中截字加...的函数
<#macro cut title count>
<#if title?length gt count>${title[0..count]}...<#else>${title?trim}</#if>
</#macro>
<@cut title=new.title count=15/>
1.输出 ${book.name}
空值判断:${book.name?exists },
${book.name?default(‘xxx’)}//默认值xxx
${ book.name!"xxx"}//默认值xxx
日期格式:${book.date?string(''yyyy-MM-dd'')}
数字格式:${book?string.number}--20
${book?string.currency}--<#-- $20.00 -->
${book?string.percent}—<#-- 20% -->
插入布尔值:
<#assign foo=ture />
${foo?string("yes","no")} <#-- yes -->
2.逻辑判断
a:
<#if condition>...
<#elseif condition2>...
<#elseif condition3>......
<#else>...
其中空值判断可以t;
b:
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
...
<#case refValueN>
...
<#break>
<#default>
...
</#switch>
3.循环读取
<#list sequence as item>
...
</#list>
空值判断<#if bookList?size = 0></#list>
e.g.
<#list employees as e>
${e_index}. ${e.name}
</#list>
输出:
1. Readonly
2. Robbin
4.FreeMarker 3 宏/模板
宏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: ? ? ?
1.字符串:使用单引号或双引号限定;如果包含特殊字符需要转义符:${"It's \"quoted\" andthis is a backslash: \\"}
有一类特殊的字符串:${r"C:\foo\bar"},输出结构为:C:\foo\bar,在引号前面加r被认为是纯文本。
2.数字:直接输入,不需要引号。${08}, ${+8}, ${8.00} and ${8} 都是相同的
3.布尔值:true和false,不使用引号
4.Sequences(序列)
a.由逗号分隔的变量列表,由方括号限定,类似java中的一维数组:
例一:["winter", "spring", "summer", "autumn"]
例二:[2 + 2, [1, 2, 3, 4], "whatnot"] (可以嵌套)
例三:2..5,等同于[2, 3, 4, 5];5..2,等同于[5,4,3,2]。
注意方括号是不需要的。 (另外写法)
b.获取Sequence(序列)中元素-使用[startindex..endindex]
例如:seq中存储了"a", "b", "c", "d","e",
那么seq[1..2]包含了b和c两个值。
c.Sequences(序列)元素的遍历
<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>
${user}
</#list>
5.Hashes(散列)
由逗号分隔的键-值列表,由大括号限定,键和值之间用冒号分隔:{"name":"green mouse", "price":150},键和值都是表达式,但是键必须是字符串。
获取变量-${variable},变量名只能是字母、数字、下划线、$、@和#的组合,且不能以数字开头。可以使用.variablename语法访问FreeMarker内置变量。 下列表达式是等价的:
book.author.name
book["author"].name
book.author.["name"]
book["author"]["name"]
6.字符串操作
{"Hello ${user}!"} <==> ${"Hello " + user + "!"}
${"${user}${user}${user}${user}"} <==> ${user + user + user + user}
${…}只能在文本中使用,下面是错误的代码:
<#if ${isBig}>Wow!</#if>
<#if "${isBig}">Wow!</#if> //此处的代码也是错误的,因为if指令需要的是boolean,实际的却是个字符串
子字符串的操作,
<#assign user="Big Joe">
${user[0]}${user[4]} <==> BJ
${user[1..4]} <==> ig J
注意: 操作符两边必须是数字;使用"+"时,如果一边是数字,一边是字符串,就会自动将数字转换为字符串。
7.使用内建的指令int获得整数部分:
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
8.比较操作符-<#if expression>...</#if>
1.)使用=(或==,完全相等)测试两个值是否相等,使用!= 测试两个值是否不相等
2.)=和!=两边必须是相同类型的值,否则会产生错误,例如<#if 1 = "1">会引起错误
3.)Freemarker是精确比较,所以"x"、"x "和"X"是不相等的
4.)对数字和日期可以使用<、<=、>和>=,但不能用于字符串
5.)由于Freemarker会将>解释成FTL标记的结束字符,所以对于>和>=可以使用括号来避免这种情况,例如& lt;#if (x > y)>,另一种替代的方法是,使用lt、lte、gt和gte来替代<、<=、>和>=
6.)逻辑操作符-&&(and)、||(or)、!(not),只能用于布尔值,否则会产生错误
<#if x < 12 && color = "green">
We have less than 12 things, and they are green.
</#if>
<#if !hot> <#-- here hot must be a boolean -->
It's not hot.
</#if>
9.内置函数-用法类似访问hash(散列)的子变量,只是使用"?"替代".",
例如:user?upper_case
下面列出常用的一些函数:
对于字符串
html-对字符串进行HTML编码
cap_first-使字符串第一个字母大写
lower_case-将字符串转换成小写
trim-去掉字符串前后的空白字符
对于Sequences(序列)
size-获得序列中元素的数目
对于数字
int-取得数字的整数部分(如-1.9?int的结果是-1)
10.方法的调用
${repeat("What", 3)}
${repeat(repeat("x", 2), 3) + repeat("What", 4)?upper_case}
结果:
WhatWhatWhat
xxxxxxWHATWHATWHATWHAT
11.操作符优先顺序
后缀 [subvarName] [subStringRange] . (methodParams)
一元 +expr、-expr、!
内建 ?
乘法 *、 / 、%
加法 +、-
关系 <、>、<=、>=(lt、lte、gt、gte)
相等 =、!=
逻辑 &&
逻辑 ||
数字范围 ..
12.一些常用控制语句:
1)if, else, elseif
Java代码
1. <#if x == 1>
2. x is 1
3. <#if y == 1>
4. and y is 1 too
5. <#else>
6. but y is not
7. </#if>
8. <#else>
9. x is not 1
10. <#if y < 0>
11. and y is less than 0
12. </#if>
13. </#if>
2)switch, case, default, break
<#switch value>
Java代码
1. <#switch being.size>
2. <#case "small">
3. This will be processed if it is small
4. <#break>
5. <#case "medium">
6. This will be processed if it is medium
7. <#break>
8. <#case "large">
9. This will be processed if it is large
10. <#break>
11. <#default>
12. This will be processed if it is neither
13. </#switch>
3) list, break
Java代码
1. <#list sequence as item>
2. ${item}
3. <#if item = "xx"><#break></#if>
4. </#list>
4)include
Java代码
1. <#include "/common/navbar.html" parse=false encoding="Shift_JIS">
5)import
Java代码
1. <#import "/libs/mylib.ftl" as my>
2. <@my.copyright date="1999-2002"/>
Note: that it is possible to automatically do the commonly used imports for all templates, with the "auto imports" setting of Configuration.
6)noparse
Java代码
1. <#noparse>
2. <#list animals as being>
3. <tr><td>${being.name}<td>${being.price} Euros
4. </#list>
5. </#noparse>
7)compress
Java代码
1. <#assign x = " moo \n\n ">
2. (<#compress>
3. 1 2 3 4 5
4. ${moo}
5. test only
6.
7. I said, test only
8.
9. </#compress>)
8)escape, noescape
Java代码
1. <#assign x = "<test>">
2. <#macro m1>
3. m1: ${x}
4. </#macro>
5. <#escape x as x?html>
6. <#macro m2>m2: ${x}</#macro>
7. ${x}
8. <@m1/>
9. </#escape>
10. ${x}
11. <@m2/>
12.
13. <#escape x as x?html>
14. From: ${mailMessage.From}
15. Subject: ${mailMessage.Subject}
16. <#noescape>Message: ${mailMessage.htmlFormattedBody}</#noescape>
17. ...
18. </#escape>
19.
20. <#escape x as x?html>
21. Customer Name: ${customerName}
22. Items to ship:
23. <#escape x as itemCodeToNameMap[x]>
24. ${itemCode1}
25. ${itemCode2}
26. ${itemCode3}
27. ${itemCode4}
28. </#escape>
29. </#escape>
9)assign
Java代码
1. <#import "/mylib.ftl" as my>
2. <#assign
3. seasons = ["winter", "spring", "summer", "autumn"]
4. test = test + 1
5. bgColor="red" in my
6. >
7. <#macro myMacro>foo</#macro>
8. <#assign x>
9. <#list 1..3 as n>
10. ${n} <@myMacro />
11. </#list>
12. </#assign>
13. Number of words: ${x?word_list?size}
14. ${x}
15. <#assign x="Hello ${user}!">
10)global
Java代码
1. <#global name=value>
2. or
3. <#global name1=value1 name2=value2 ... nameN=valueN>
4. or
5. <#global name>
6. capture this
7. </#global>
11)local
note:it is similar to assign directive, but it creates or replaces local variables. This works in macro definition bodies only.
Java代码
1. <#local name=value>
2. or
3. <#local name1=value1 name2=value2 ... nameN=valueN>
4. or
5. <#local name>
6. capture this
7. </#local>
12)setting
note:The supported settings are:locale,number_format,boolean_format,
date_format, time_format, datetime_format,time_zone,url_escaping_charset,classic_compatible
Java代码
1. ${1.2}
2. <#setting locale="en_US">
3. ${1.2}
13) (<@...>)
Java代码
1. <@myRepeatMacro count=4 ; x, last>
2. ${x}. Something... <#if last> This was the last!</#if>
3. </@myRepeatMacro>
14)macro, nested, return
Java代码
1. <#macro img src extra...>
2. <img src="/context${src?html}"
3. <#list extra?keys as attr>
4. ${attr}="${extra[attr]?html}"
5. </#list>
6. >
7. </#macro>
8. <@img src="/images/test.png" width=100 height=50 alt="Test"/>
9.
10. <#macro repeat count>
11. <#list 1..count as x>
12. <#nested x, x/2, x==count>
13. </#list>
14. </#macro>
15. <@repeat count=4 ; c, halfc, last>
16. ${c}. ${halfc}<#if last> Last!</#if>
17. </@repeat>
18. <#macro test>
19. Test text
20. <#return>
21. Will not be printed.
22. </#macro>
23. <@test/>
15)function, return
Java代码
1. <#function avg nums...>
2. <#local sum = 0>
3. <#list nums as num>
4. <#local sum = sum + num>
5. </#list>
6. <#if nums?size != 0>
7. <#return sum / nums?size>
8. </#if>
9. </#function>
10. ${avg(10, 20)}
11. ${avg(10, 20, 30, 40)}
12. ${avg()!"N/A"}
16).flush
17)stop
18)ftl
19)t, lt, rt
20)nt
21)attempt, recover
Java代码
1. Primary content
2. <#attempt>
3. Optional content: ${thisMayFails}
4. <#recover>
5. Ops! The optional content is not available.
6. </#attempt>
7. Primary content continued
22)visit, recurse, fallback
三. Interpolation:由${...}或#{...}两种类型,输出计算值,可以自定义输出的格式
1)string的操作:
substring
cap_first
uncap_first
capitalize
chop_linebreak
date, time, datetime
ends_with
html
groups
index_of
j_string
js_string
last_index_of
length
lower_case
left_pad
right_pad
contains
matches
number
replace
rtf
url
split
starts_with
string (when used with a string value)
trim
upper_case
word_list
xml
2)numbers的操作:
c
string (when used with a numerical value)
round, floor, ceiling
3)dates的操作:
string (when used with a date value)
date, time, datetime
4)booleans的操作:
string (when used with a boolean value)
5)sequences的操作:
first
last
seq_contains
seq_index_of
seq_last_index_of
reverse
size
sort
sort_by
chunk
6)hashes的操作:
keys
values
7)nodes 的操作:
children
parent
root
ancestors
node_name
node_type
node_namespace
四.) 注释:<#--和-->
分享到:
相关推荐
freemark从入门到精通,freemark讲解,从小白到大明白。
本人觉得比较好的freemark学习文档
freemark学习笔记:在阅读freemark官方文档的过程中记录下的一些笔记。包括自己对freemark的理解以及实践中的经验
freemark是非常好用的模板语言,表达式丰富多彩,可以很好的和第三方框架集成,完成显示层的很好控制,感兴趣的朋友,可以下载学习!
freemark是运用java开发前端的一项技术。 能够帮助开发者简化开发难度。
1,文本:直接输出的部分 2,注释:格式部分,不会输出 3,插值:即${...}或#{...}格式的部分,将使用数据模型中的部分替代输出 ... 下面是一个FreeMarker模板的例子,包含了以上所说的4个部分 <html><br> <head><br> ...
freemark+学习资料
freemark中文资料 freemark教程 freemark基础和高级教程
都是自己总结还有在网上查的资料的,整理了一下,对于初学者还是很有帮助的,都是些基础性的东西,希望对大家有帮助!
freemark比较实用的手册,中文版 适合快速学习
PDF格式中文版手册,是学习FreeMarker的必备手册。freemark 开发指南
freemark小例子仅供参考学习 谢谢大家提供建议和资源 供大家学习交流
这个我自己学习freemark时自己写的例子,可以运行能够了解freemark简单的知识,像自动生成模板代码什么的功能这里面没有。
学习文档,主要介绍了freemark的基本运用,学习之后可以达到写出自己DEMO的效果
第20章 网络文件管理系统实例编程(struts2+hibernate+freemark) 学习J2EE的绝好质料.
FreeMarker 是一个好用的java引擎,本站为您提供的是中文教程资料合集,为您使用和学习FreeMark提供帮助,内容包括:教程、标签、参考手册、根据模版生成文件使用及语法相关小例子等
课程设计作业-基于springboot+freemark的学生学籍信息管理系统源码+sql数据库.zip 该资源内项目代码都是经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 本项目适合计算机相关专业(如计科、人工智能、...
适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究...