一,符号的意义
1.0,单问号
?后面要加关键字,例如:<#if object?exists>object对象不为空</#if> <#if str??>${str?string}</#if><#--将str以字符串形式显示--> ${nowDate?time}<#--将现有时间以时间的格式显示,显示结果如:15:13:05--> ${nowDate?date}<#--以日期格式显示,如:2011-4-28-->(date的格式可以在freemarker.properties文件中配置)
单问号后面跟的是函数
其实?exists就相当于??
1.1,双问号
<#if orderParam.quliJycs?? && orderParam.quliJycs == ‘1’>已上传<#else>请上传</#if>
其中双问号意思就相当于 orderParam.quliJycs?exists 是否存在 ,判断为非空的,这句话的意思就是说如果orderParam.quliJycs存在并且值为1则是已上传。
1.1.1,三个问号
${(num.color)???string}
前连个问号是判断是否存在,后面一个问号是让它以String的类型输出
1.2,单个感叹号
${(user.name)!""} ${(user.name)!}
3,${user.name?default(‘xxx’)}//默认值xxx
4 ${(user.name)!"默认值"}
第一行是说如果为空就让值为空
第二行就是如果为空不会报错
第三行则是如果为空则值为默认值
第三行和第四行的作用是一样的。
但是前提条件都是user对象不能为空,这句话主要针对的是它的值
1.3,双感叹号
${abc!!},<#list strList!! as str >
比如${list strList as str},如果strList不存在,则freemarker是会报错的
而如果加上<#list strList!! as str >,则freemarker就会对忽略掉空变量而不会报错
1.4,判断非空
1、if和”??“
<#if age??>
无年龄值
<#/if>
2、$和!
${age!’0′}
如果age为null,默认给’0′
二,循环语句的应用
2.1,if else 语句
<#if student.studentAge lt 12> ${student.studentName}不是一个初中生 <#elseif student.studentAge lt 15> ${student.studentName}不是一个高中生 <#elseif student.studentAge lt 18> ${student.studentName}不是一个大学生 <#else> ${student.studentName}是一个大学生 </#if>
<#if condition>... <#elseif condition2>... <#elseif condition3>...... <#else>... </#if>
2.2,switch的应用
字符串
<#switch being.size> <#case "small"> This will be processed if it is small <#break> <#case "medium"> This will be processed if it is medium <#break> <#case "large"> This will be processed if it is large <#break> <#default> This will be processed if it is neither </#switch>
数字
<#switch x> <#case x = 1> 1 <#case x = 2> 2 <#default> d </#switch>
格式
<#switch value> <#case refValue1> ... <#break> <#case refValue2> ... <#break> ... <#case refValueN> ... <#break> <#default> ... </#switch>
三,集合
2.1,集合的长度
<#if student?size != 0></#if> 判断=的时候,注意只要一个=符号,而不是==
2.2,遍历list集合
User类 public class User{ private String username; private (省略set和get方法) } user.ftl <#--Freemarker遍历list--> 简单遍历list: <#list userList as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} </#list> <#--Freemarker遍历list并应用list隐含变量item_index--> item_index使用: <#list userList as user> 第${user_index+1}个用户 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} </#list> <#--Freemarker遍历list并应用list隐含变量item_has_next--> item_has_next,size使用: <#list userList as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} <#if !user_has_next> 共有${userList?size}最后一个用户是:${user.userName} </#if> </#list> <#--Freemarker遍历list并按用户年龄升序排序--> 按用户年龄升序排序: <#list userList?sort_by("age") as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} </#list> <#--Freemarker遍历list并按用户年龄降序排序--> 按用户年龄降序排序: <#list userList?sort_by("age")?reverse as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} </#list> <#--Freemarker遍历list当用户年龄大于21岁时,停止输出--> list中应用break: <#list userList?sort_by("age")?reverse as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} <#if (user.age>21) > <#break> </#if> </#list>
2.3,遍历map集合一
假设selectDateModel 是我们后台返回的map<String, String>; <#list selectDateModel?keys as key> <option value="${key}">${selectDateModel[key]}</option> </#list>
2.4,遍历map集合二
<#--freemarker map的应用--> <#--创建一个map,注意在freemarker中,map的key只能是字符串来作为key--> <#assign userMap={"1","刘德华","2":"张学友"}/> <#--获取map中的值--> ${userMap["1"]} <#--获取map的keys--> <#assign keys=userMap?keys/> <#--遍历map 首选获取key的集合--> <#list keys as key> key:${key}-value:${userMap["${key}"]} </#list> </br> <#--直接遍历map的第二种方式--> <#list userMap?keys as key> key:${key}--value:${userMap["${key}"]} </#list> </br> <#--直接遍历map的values--> <#list userMap?values as value> ${value} </#list>
3,其它
3.1,判断null,””
<#if object.param??&&object.param!="">${object.param!}<#else>请选择</#if>