Freemarker 开发指南.docx
- 文档编号:28930652
- 上传时间:2023-07-20
- 格式:DOCX
- 页数:25
- 大小:24.54KB
Freemarker 开发指南.docx
《Freemarker 开发指南.docx》由会员分享,可在线阅读,更多相关《Freemarker 开发指南.docx(25页珍藏版)》请在冰豆网上搜索。
Freemarker开发指南
Freemark开发指南
A概念
最常用的3个概念
sequence序列,对应java里的list、数组等非键值对的集合
hash键值对的集合
namespace对一个ftl文件的引用,利用这个名字可以访问到该ftl文件的资源
B指令
if,else,elseif
语法
<#ifcondition>
...
<#elseifcondition2>
...
<#elseifcondition3>
...
...
<#else>
...
#if>
用例
<#ifx=1>
xis1
#if>
<#ifx=1>
xis1
<#else>
xisnot1
#if>
switch,case,default,break
语法
<#switchvalue>
<#caserefValue1>
...
<#break>
<#caserefValue2>
...
<#break>
...
<#caserefValueN>
...
<#break>
<#default>
...
#switch>
用例
字符串
<#switchbeing.size>
<#case"small">
Thiswillbeprocessedifitissmall
<#break>
<#case"medium">
Thiswillbeprocessedifitismedium
<#break>
<#case"large">
Thiswillbeprocessedifitislarge
<#break>
<#default>
Thiswillbeprocessedifitisneither
#switch>
数字
<#switchx>
<#casex=1>
1
<#casex=2>
2
<#default>
d
#switch>
如果x=1输出12,x=2输出2,x=3输出d
list,break
语法
<#listsequenceasitem>
...
<#ifitem="spring"><#break>#if>
...
#list>
关键字
item_index:
是list当前值的下标
item_has_next:
判断list是否还有值
用例
<#assignseq=["winter","spring","summer","autumn"]>
<#listseqasx>
${x_index+1}.${x}<#ifx_has_next>,#if>
#list>
输出
1.winter,
2.spring,
3.summer,
4.autumn
include
语法
<#includefilename>
or
<#includefilenameoptions>
options包含两个属性
encoding=”GBK”编码格式
parse=true是否作为ftl语法解析,默认是true,false就是以文本方式引入.注意在ftl文件里布尔值都是直接赋值的如parse=true,而不是parse=”true”
用例
/common/copyright.ftl包含内容
Copyright2001-2002${me}
Allrightsreserved.
模板文件
<#assignme="JuilaSmith">
Sometest
Yeah.
<#include"/common/copyright.ftl"encoding=”GBK”>
输出结果
Sometest
Yeah.
Copyright2001-2002JuilaSmith
Allrightsreserved.
Import
语法
<#importpathashash>
类似于java里的import,它导入文件,然后就可以在当前文件里使用被导入文件里的宏组件
用例
假设mylib.ftl里定义了宏copyright那么我们在其他模板页面里可以这样使用
<#import"/libs/mylib.ftl"asmy>
<@my.copyrightdate="1999-2002"/>
"my"在freemarker里被称作namespace
compress
语法
<#compress>
...
#compress>
用来压缩空白空间和空白的行
用例
<#assignx="moo\n\n">
(<#compress>
12345
${moo}
testonly
Isaid,testonly
#compress>)
输出
(12345
moo
testonly
Isaid,testonly)
escape,noescape
语法
<#escapeidentifierasexpression>
...
<#noescape>...#noescape>
...
#escape>
用例
主要使用在相似的字符串变量输出,比如某一个模块的所有字符串输出都必须是html安全的,这个时候就可以使用该表达式
<#escapexasx?
html>
Firstname:
${firstName}
<#noescape>Lastname:
${lastName}#noescape>
Maidenname:
${maidenName}
#escape>
相同表达式
Firstname:
${firstName?
html}
Lastname:
${lastName}
Maidenname:
${maidenName?
html}
assign
语法
<#assignname=value>
or
<#assignname1=value1name2=value2...nameN=valueN>
or
<#assignsameasabove...innamespacehash>
or
<#assignname>
capturethis
#assign>
or
<#assignnameinnamespacehash>
capturethis
#assign>
用例
生成变量,并且给变量赋值
给seasons赋予序列值
<#assignseasons=["winter","spring","summer","autumn"]>
给变量test加1
<#assigntest=test+1>
给mynamespage赋予一个变量bgColor,下面可以通过my.bgColor来访问这个变量
<#import"/mylib.ftl"asmy>
<#assignbgColor="red"inmy>
将一段输出的文本作为变量保存在x里
下面的阴影部分输出的文本将被赋值给x
<#assignx>
<#list1..3asn>
${n}<@myMacro/>
#list>
#assign>
Numberofwords:
${x?
word_list?
size}
${x}
<#assignx>Hello${user}!
#assign>error
<#assignx=”Hello${user}!
”>true
同时也支持中文赋值,如:
<#assign语法>
java
#assign>
${语法}
打印输出:
java
global
语法
<#globalname=value>
or
<#globalname1=value1name2=value2...nameN=valueN>
or
<#globalname>
capturethis
#global>
全局赋值语法,利用这个语法给变量赋值,那么这个变量在所有的namespace中是可见的,如果这个变量被当前的assign语法覆盖如<#globalx=2><#assignx=1>在当前页面里x=2将被隐藏,或者通过${.global.x}来访问
setting
语法
<#settingname=value>
用来设置整个系统的一个环境
locale
number_format
boolean_format
date_format,time_format,datetime_format
time_zone
classic_compatible
用例
假如当前是匈牙利的设置,然后修改成美国
${1.2}
<#settinglocale="en_US">
${1.2}
输出
1,2
1.2
因为匈牙利是采用“,”作为十进制的分隔符,美国是用“.”
macro,nested,return
语法
<#macronameparam1param2...paramN>
...
<#nestedloopvar1,loopvar2,...,loopvarN>
...
<#return>
...
#macro>
用例
<#macrotestfoobar="Bar"baaz=-1>
Testtext,andtheparams:
${foo},${bar},${baaz}
#macro>
<@testfoo="a"bar="b"baaz=5*5-2/>
<@testfoo="a"bar="b"/>
<@testfoo="a"baaz=5*5-2/>
<@testfoo="a"/>
输出
Testtext,andtheparams:
a,b,23
Testtext,andtheparams:
a,b,-1
Testtext,andtheparams:
a,Bar,23
Testtext,andtheparams:
a,Bar,-1
定义循环输出的宏
<#macrolisttitleitems>
${title?
cap_first}:
- ${x?
cap_first}
#list>
<#listitemsasx>
#macro>
<@listitems=["mouse","elephant","python"]title="Animals"/>
输出结果
Animals:
- Mouse
- Elephant
- Python
包含body的宏
<#macrorepeatcount>
<#list1..countasx>
<#nestedx,x/2,x==count>
#list>
#macro>
<@repeatcount=4;chalfclast>
${c}.${halfc}<#iflast>Last!
#if>
@repeat>
输出
1.0.5
2.1
3.1.5
4.2Last!
t,lt,rt
语法
<#t>去掉左右空白和回车换行
<#lt>去掉左边空白和回车换行
<#rt>去掉右边空白和回车换行
<#nt>取消上面的效果
C一些常用方法或注意事项
表达式转换类
${expression}计算expression并输出
#{expression}数字计算#{expression;format}安格式输出数字format为M和m
M表示小数点后最多的位数,m表示小数点后最少的位数如#{121.2322;m2M2}输出121.23
数字循环
1..5表示从1到5,原型number..number
对浮点取整数
${123.23?
int}输出123
给变量默认值
${var?
default(“helloworld
”)?
html}如果varisnull那么将会被helloworld
替代
判断对象是不是null
<#ifmouse?
exists>
Mousefound
<#else>
也可以直接${mouse?
if_exists})输出布尔形
常用格式化日期
openingTime必须是Date型,详细查看freemarker文档Reference->build-inreferece->build-infordate
${openingTime?
date}
${openingTime?
date_time}
${openingTime?
time}
添加全局共享变量数据模型
在代码里的实现
cfg=Configuration.getDefaultConfiguration();
cfg.setSharedVariable("global","yougood");
页面实现可以通过global指令,具体查看指令里的global部分
直接调用java对象的方法
${object.methed(args)}
字符串处理(内置方法)
html安全输出
“abc