freemarker基础
# freemarker模板概述
FreeMarker模板文件主要有5个部分组成: 1、数据模型:模板能用的所有数据 2、文本,直接输出的部分 3、注释,即<#--...-->格式不会输出 4、 插值(Interpolation):即${..}或者#{..}格式的部分,将使用数据模型中的部分替代输出 5、FTL指令:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出。
# freemarker常用标签
在FreeMarker模板中可以包括下面几个特定部分: 1、${…}:称为interpolations,FreeMarker会在输出时用实际值进行替代。 ${name}可以取得root中key为name的value。 ${person.name}可以取得成员变量为person的name属性 2、**<#…>:FTL标记(FreeMarker模板语言标记):类似于HTML标记,为了与HTML标记区分 3、 <@>:宏,自定义标签 4、注释:包含在<#--和-->(而不是)之间
# freemarker的指令
# if、else、elseif
<#if condition>
....
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
2
3
4
5
6
7
8
9
集合的判断 gt gte lt lte
<#if couponRecords?? && (couponRecords?size gt 0) > </#if>
// 日期的判断
<#if m.startTime?? || m.startTime!=null>${(m.startTime)?string("yyyy-MM-dd HH:mm:ss")}</#if>
2
3
4
# list、break指令
<#list sequence as item>
...
</#list>
除此之外,迭代集合对象时,还包括两个特殊的循环变量:
a、item_index:当前变量的索引值。
b、item_has_next:是否存在下一个对象
也可以使用<#break>指令跳出迭代
<#list ["星期一","星期二","星期三","星期四","星期五"] as x>
${x_index +1}.${x} <#if x_has_next>,</#if>
<#if x = "星期四"><#break></#if>
</#list>
2
3
4
5
6
7
8
9
10
11
# include指令
include指令的作用类似于JSP的包含指令,用于包含指定页,include指令的语法格式如下
<#include filename [options]></#include>
在上面的语法格式中,两个参数的解释如下
- a、filename:该参数指定被包含的模板文件
- b、options:该参数可以省略,指定包含时的选项,包含encoding和parse两个选项,encoding指定包含页面时所使用的解码集,而parse指定被包含是否作为FTL文件来解析。如果省略了parse选项值,则该选项值默认是true
# assign指令
它用于为该模板页面创建或替换一个顶层变量
<#assign name = zhangsan />
# freemarker常用函数
# 1、判断
- 空值判断 :
${(emObj!="")?string("ok","error")}
- null值判断:
${(nullObj??)?string("ok","error")}
- null值判断:
${(nullObj?exists)?string}
- 空值+null判断:
<#if emObj?? && emObj!="">不为空<#else>为空</#if>
- 对象值非空判断
<#if person.phone?? && person.phone!="">不为空<#else>为空</#if>
# 2、内置函数
?html html字符转义
?cap_first 字符串的第一个字母变为大写形式
?lower_case 字符串的小写形式
?upper_case 字符串的大写形式
?trim 去掉字符串首尾的空格
?substring 截字符串
?lenth 取长度
?size 序列中元素的个数
?int 数字的整数部分(比如- 1.9?int 就是- 1)
?replace 字符串替换
2
3
4
5
6
7
8
9
10
11
12
参考: 内建函数参考 - FreeMarker 中文官方参考手册 (foofun.cn) (opens new window)
# 应用:
截取字符串,显示省略号
var buyerNickName = "【<#if buyer.nickName!?length gt 10> ${buyer.nickName?substring(0,10)}...<#else> ${buyer.nickName!}</#if>】";
<#if isHelper>
Gmzx.Msg.alert( buyerNickName +' 正在邀请您帮她砍价,一起加油哦!');
</#if>
2
3
4
# freemarker中使用session、request
# 1、Request
用于获取Request对象中的attribute对象。
例如:${Request["myRequestAttribute"]} 这样是直接在页面输出属性值。相当于request.getAtrribute("myRequestAttribute");
如果要对这个值进行判断就必须使用如下格式:<#if Request["myRequestAttribute"]="edit">
或者 : ${Request["myRequestAttribute"]!"default value"}
# 2、Session
用于获取Session 对象中的attribute对象。
用法参照Request的用法。
# 3、Application
用于获取 Application(ServletContext)对象中的attribute对象。用法参照Request的用法。
# 4、RequestParameters
用 于获取Request对象的parameter参数(浏览器端发送的请求数据)
例如:${RequestParameters["myRequestAttribute"]}等同于 request.getParameter("myRequestAttribute");
# 5、Parameters
属性获取,依次从 RequestParameters、Request、Session、Application对象中获取对应属性/参数,一旦获取,则不再向下查找。例如:${Parameters["myRequestAttribute"]}
Freemarker可以直接取pageContext,requestAttribute,session,application中的数据,就是不能取requestParameter;
参考文章 :
FreeMarker 中文官方参考手册 (foofun.cn) (opens new window)
# freemarker使用小技巧
1、判断变量是否存在 <#if userName??> 判断userName是否存在,为null和没这个变量都代表不存在
2、整数转化为字符串,字符串转化为整数 ${xx?string} //字符串 ${xx?number}//整数
3、在比较运算符的两端如果不是同一种数据类型,freemarker会报错 a=1,b="1" <#if a==b??>这个时候就会报错 改成这样:<#if a?string==b??>
4、当一个变量为null或者不存在时,直接取这个变量的值会报错
userName=null;
${userName}:这样写就会报错
${userName!''}:这样写ok,代表如果不存在给个默认值空字符串
但是${user.userName}这种情况下user为null,只能这样写 ${(user.userName)!''}
5、获取字符串的长度 <#if user.userName?length lt 8>
6、获取集合的大小 <#if userList?size lt 8>
7、保留两位有效数字 ${x?string("0.##")}
8、设置上下文路径 <#assign ctxPath=request.contextPath>
9、集合遍历
<div style="overflow-y:auto;height:300px">
<table border="0" cellspacing="0" cellpadding="0" class="success-big-table">
<tr>
<th ><input type="checkbox" id="selectAll" name="selectAll" οnclick="selectAll(this)"/></th>
<th >用户名</th>
<th >地址</th>
<th >年龄</th>
</tr>
<#if page?? && page.list?? &&page.list?size gt 0>
<#list page.list as user>
<tr>
<td>
<input type="checkbox" name="checkbox" value="${user.userName!''}"/>
</td>
<td>${user.userName!''}</td>
<td>${user.addreee!''}</td>
<td>${user.age!''}</td>
</tr>
</#list>
<#else>
<tr>
<td colspan="4">查询无结果</td>
<tr>
</#if>
</table>
</div>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26