一、? Sequence的内置函数
1.???? sequence?first 返回sequence的第一个值。
2.???? sequence?last? 返回sequence的最后一个值。
3.???? sequence?reverse 将sequence的现有顺序反转,即倒序排序
4.???? sequence?size??? 返回sequence的大小
5.???? sequence?sort??? 将sequence中的对象转化为字符串后顺序排序
6.???? sequence?sort_by(value) 按sequence中对象的属性value进行排序
二、????????? Hash的内置函数
1.???????????????????? hash?keys 返回hash里的所有key,返回结果为sequence
2.???????????????????? hash?values 返回hash里的所有value,返回结果为sequence
例如:
<#assign user={“name”:“hailang”, “sex”:“man”}>
<#assign keys=user?keys>
<#list keys as key>
${key}=${user[key]}
</#list>
三、????????? 操作字符串函数
1.???????????????????? substring(start,end)从一个字符串中截取子串
start:截取子串开始的索引,start必须大于等于0,小于等于end
end: 截取子串的长度,end必须大于等于0,小于等于字符串长度,如果省略该参数,默认为字符串长度。
例子:
${‘str’?substring(0)}à结果为str
${‘str’?substring(1)}à结果为tr
${‘str’?substring(2)}à结果为r
${‘str’?substring(3)}à结果为
${‘str’?substring(0,0)}à结果为
${‘str’?substring(0,1)}à结果为s
${‘str’?substring(0,2)}à结果为st
${‘str’?substring(0,3)}à结果为str
2.???????????????????? cap_first 将字符串中的第一个单词的首字母变为大写。
${‘str’?cap_first}à结果为Str
3.???????????????????? uncap_first将字符串中的第一个单词的首字母变为小写。
${‘Str’?cap_first}à结果为str
4.???????????????????? capitalize将字符串中的所有单词的首字母变为大写
${‘str’? capitalize}à结果为STR
5.???????????????????? date,time,datetime将字符串转换为日期
例如:
<#assign date1=”2009-10-12”?date(“yyyy-MM-dd”)>
<#assign date2=”9:28:20”?time(“HH:mm:ss”)>
<#assign date3=” 2009-10-12 9:28:20”?time(“HH:mm:ss”)>
${date1}à结果为2009-10-12
${date2}à结果为9:28:20
${date3}à结果为2009-10-12 9:28:20
注意:如果指定的字符串格式不正确将引发错误。
6.???????????????????? ends_with 判断某个字符串是否由某个子串结尾,返回布尔值。
${“string”?ends_with(“ing”)?string} 返回结果为true
注意:布尔值必须转换为字符串才能输出
7.???????????????????? html 用于将字符串中的<、>、&和“替换为对应得<>":&
8.???????????????????? index_of(substring,start)在字符串中查找某个子串,返回找到子串的第一个字符的索引,如果没有找到子串,则返回-1。
Start参数用于指定从字符串的那个索引处开始搜索,start为数字值。
如果start大于字符串长度,则start取值等于字符串长度,如果start小于0, 则start取值为0。
${“string”?index_of(“in”) à结果为3
${“string”?index_of(“ab”) à结果为-1
9.???????????????????? length返回字符串的长度 ${“string”?length}à结果为6
10.????????????????? lower_case将字符串转为小写
${“STRING”?lower_case}à结果为string
11.????????????????? upper_case将字符串转为大写
${“string”?upper_case}à结果为STRING
12.????????????????? contains 判断字符中是否包含某个子串。返回布尔值
${“string”?contains(“ing”)?string} à结果为true
注意:布尔值必须转换为字符串才能输出
13.????????????????? number将字符串转换为数字
${“111.11”?number}à结果为111.11
14.????????????????? replace用于将字符串中的一部分从左到右替换为另外的字符串。
${“strabg”?replace(“ab”,”in”)} à结果为string
15.?????????????????? split使用指定的分隔符将一个字符串拆分为一组字符串
<#list “This|is|split”?split(“|”) as s>
${s}
</#list>
结果为:
This
is
split
16.????????????????? trim 删除字符串首尾空格 ${“? String ”?trim} à结果为String
四、????????? 操作数字
1.???????????????????? c 用于将数字转换为字符串
${123?c} à结果为123
2.???????????????????? string用于将数字转换为字符串
Freemarker中预订义了三种数字格式:number,currency(货币)和percent(百分比)其中number为默认的数字格式转换
例如:
<#assign tempNum=20>
${tempNum}???
${tempNum?string.number}或${tempNum?string(“number”)} à结果为20
${tempNum?string.currency}或${tempNum?string(“currency”)} à结果为¥20.00
${tempNum?string. percent}或${tempNum?string(“percent”)} à结果为2,000%
五、??? 操作布尔值
string 用于将布尔值转换为字符串输出
true转为“true”,false转换为“false”
foo?string(“yes”,”no”)如果布尔值是true,那么返回“yes”,否则返回no
==============常用语法=================
三.
EG.一个对象BOOK
1.输出 ${book.name}
空值判断:${book.name?if_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 condit
inc 发布于2007-09-08 16:18:57
ion2>...
<#elseif condition3>......
<#else>...
其中空值判断可以写成<#if book.name?? >
</#if>
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>
<#if mole?default(0)!=0>
=============用来压缩空白空间和空白的行===============
用例
<#assign x = "??? moo ?? ">
(<#compress>
1 2 3?? 4??? 5
${moo}
test only
I said, test only
)
输出
(1 2 3 4 5
moo
test only
I said, test only)
==============Sequence内置的计数器==============
3. Sequence内置的计数器: xxx_index
用途:显示序号
模板:
<#list employees as e>
${e_index}. ${e.name}
</#list>
输出:
1. Readonly
2. Robbin
==============Sequence内置的分段器=====================
4. Sequence内置的分段器: chunk
用途:某些比较BT的排版需求
模板:
<#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>
<#list seq?chunk(4) as row>
<ul>
<li><#list row as cell>${cell} </#list></li>
</ul>
</#list>
<#list seq?chunk(4, '-') as row>
<tr>
<td><#list row as cell>${cell} </#list></td>
</tr>
</#list>
输出:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>
<ul>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
<ul>
<li>i</li>
<li>j</li>
</ul>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>e</td>
<td>f</td>
<td>g</td>
<td>h</td>
</tr>
<tr>
<td>i</td>
<td>j</td>
<td>-</td>
<td>-</td>
</tr>
?
=====================freemarker的list================================
Scalar String:${scalarString}???
Scalar Number:${scalarNumber}
Object is:${scalarObject}
List使用样例-List元素为Scalar对象:
<#list scalarList as value0>
??? Scalar List值:${value0}
</#list>
List使用样例-List元素为User对象:
<#list userList as listUser>
??? List对象User Id值:${listUser.userId}
</#list>
Map使用样例-Map Values元素为Scalar :
<#list scalarMap?keys as mykey>
??? Scalar Map key is :${mykey}
??? Scalar Map value is:${scalarMap[mykey]}
</#list>
Map使用样例-Map Values元素为User对象:
<#list userMap?keys as key1>
??? <#assign mapUser="${userMap[key1]}" >
??? User Object is :${mapUser}
??? <#--
??? 以下方法有问题
??? User is :${mapUser.userId} <br>
??? -->
</#list>
==============FreeMarker中list排序==============
升序:
<#list list?sort_by("time") as v>
</#list>
降序:
<#list list?sort_by("time") as v>
</#list>
?
===============freemarker在模板中定义变量==============
在模板中定义的变量有三种类型:
plain变量:可以在模板的任何地方访问,包括include指令插入的模板,使用assign指令创建和替换
<#include "/WEB-INF/index/top.html">
==============freemarker.properties配置================
(1)解决输出中文乱码问题:
default_encoding=UTF-8
locale=zh_CN
number_format=#
(2)提高freemarker的性能
template_update_delay=60000
(3)freemarker的标签种类:
${..}
# 代表是FTL tags
<#if ...></#if>
<#list totalList as elementObject>...</#list>
@ ,代表用户自定义的标签
<#-- --> 注释标签,注意不是<!-- -->
=====================将图片整除换行====== <#if (u_index+1)%4=0>当图片超过五个就换行=======
?? <table width="100%">
????????????????????? <tr>
??????????????????????? <td><table width="100%">
????????????????????????? <tr>
????????????? <#if map["最新-图片"]?exists>
???? <#list map["最新-图片"] as u>
???? <#if (u_index+1)%4=0>
??????????????? <td><table width="70" height="65" border="0" cellpadding="0" cellspacing="0">
????????????????? <tr>
??????????????????? <td align="center" valign="middle" bgcolor="#6F7074">
??????????????????? <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 "><img src="${u.chartpath?default('')}" width="67" height="41" border="0" /></a></td>
????????????????? </tr>
????????????????? <tr>
??????????????????? <td align="center" bgcolor="#6F7074">
??????????????????? <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 " class="tableff">
??????????????????? <@jf.greet title=u.title len=4 /></a>
??????????????????? </td>
????????????????? </tr>
??????????????? </table></td></tr><tr >
???? <#else>
???? <td><table width="70" height="65" border="0" cellpadding="0" cellspacing="0">
????????????????? <tr>
??????????????????? <td align="center" valign="middle" bgcolor="#6F7074">
??????????????????? <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 "><img src="${u.chartpath?default('')}" width="67" height="41" border="0" /></a></td>
????????????????? </tr>
????????????????? <tr>
??????????????????? <td align="center" bgcolor="#6F7074">
??????????????????? <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 " class="tableff">
??????????????????? <@jf.greet title=u.title len=4 /></a>
??????????????????? </td>
????????????????? </tr>
??????????????? </table></td>
???? </#if>
???? </#list>
???? </#if>
????????????? </tr>????????????
??????????? </table>
??????????????????? </td>
????????????????? </tr>
??????????????? </table>
????????????????? </tr>
??????????????? </table></td>
???? </#if>
???? </#list>
???? </#if>
????????????? </tr>????????????
??????????? </table>
??????????????????? </td>
????????????????? </tr>
??????????????? </table>
===============================freemarker中在application
${Application.web_title}
在类里:
list=dser.indexvideo(cvo);
if(list.size()!=0){
map.put("最新",list);}
在html静态页里:
<#import "/WEB-INF/ftl/index.ftl" as jf>
(index.ftl
<#macro greet title len>
<#if title?length != 0>
<#if (title?length>len)>
${title[0..len-1]} <#else>
${title?trim}
</#if>
<#else>
${title?default("")}
</#if>
</#macro>
<#--时间比较-->
<#macro vstime start end>
<#if start?default("")==""||end?default("")=="">
--
<#else>
<#assign starts=start?replace("-","") >
<#assign ends=end?replace("-","") >
<#if (starts?number>ends?number)>
<font color="red">以过期</font>
<#else>
未过期
</#if>
</#if>
</#macro>
)
<#if map["最新"]?exists>
<#list map["最新"] as u>
${u.filepath?default('')}
<@jf.greet title=u.title len=6 />
</#list></#if>