Freemarker的使用
2016-07-04 15:40
169 查看
1.必须通过后台转发到jsp才能起作用,直接访问jsp无效
2.要将变量设置到request.setAttribute中,通过${变量名称}进行引用
3.判断变量是否存在
<#if firstTemp?exists> 或者 <#if firstTemp??>
4.指令的括号
<#if firstTemp?exists> 与 <#if firstTemp?exists > 右边可以多出空格
5.日期格式化
${date?string("yyyy-MM-dd HH:mm:ss")}
若date可能不存在
${(date?string("yyyy-MM-dd HH:mm:ss"))!''}
6.判断字符串长度
<#if myString?length>10>
7.list指令
<#list myList as item>
${item_index}表示索引
${item.myprop}表示item的属性
${item_has_next}表示是否有下一个元素
</#list>
8.空白问题
${ctx } <==> ${ctx}
<#if a>b > <==> <#if a>b>
9.Map变量使用
${map['mykey']}
<#if map['mykey']>
10.根据索引使用list类型变量(索引从0开始)
${myList[1]}
<#if myList[1] == "ABC">
11.引用别的jsp
<@include_page path="path/to/some.jsp"/>
12.如果引用的是freemarker的模板文件,那么使用 <#include>
<#include "/common/navbar.html" parse=false encoding="Shift_JIS">
默认parse为true,当成freemarker的模板文件
http://freemarker.org/docs/ref_directive_include.html
13.<#if>,<#list> 中的不能使用${},而是直接使用变量名称
14.boolean(true/false)变量不能直接输出,可以这样输出 ${myBool?c} 或者 ${myBool?string('yes','no')}
15.注释采用<#--和-->, jsp的注释方式不能用
16.JSON字符串的使用
<#assign dataJson = ret?eval />
ret是JSONObject,通过setAttr("ret",ret")设置过
json array长度的判断 <#if (ret.arr?size==0)> 或 ${ret.arr?size}
在#if指令中使用,要用括号括起来,否则会出错,即类似 <#if user?? && (ret.arr?size>0)>
list类型数据,也可以这样计算长度
17.在变量为null或未设置时,显示默认值
${user!""} user为null是显示空白字符串
${(user.name)!""} user或user.name为null时显示空白字符串
在#if指令上也可以用类似用法,<#if (user.name)??> 表示user或者user.name为null
18.设置自增变量
<#assign i = 1 />
<#assign i = i+1 />
2.要将变量设置到request.setAttribute中,通过${变量名称}进行引用
3.判断变量是否存在
<#if firstTemp?exists> 或者 <#if firstTemp??>
4.指令的括号
<#if firstTemp?exists> 与 <#if firstTemp?exists > 右边可以多出空格
5.日期格式化
${date?string("yyyy-MM-dd HH:mm:ss")}
若date可能不存在
${(date?string("yyyy-MM-dd HH:mm:ss"))!''}
6.判断字符串长度
<#if myString?length>10>
7.list指令
<#list myList as item>
${item_index}表示索引
${item.myprop}表示item的属性
${item_has_next}表示是否有下一个元素
</#list>
8.空白问题
${ctx } <==> ${ctx}
<#if a>b > <==> <#if a>b>
9.Map变量使用
${map['mykey']}
<#if map['mykey']>
10.根据索引使用list类型变量(索引从0开始)
${myList[1]}
<#if myList[1] == "ABC">
11.引用别的jsp
<@include_page path="path/to/some.jsp"/>
12.如果引用的是freemarker的模板文件,那么使用 <#include>
<#include "/common/navbar.html" parse=false encoding="Shift_JIS">
默认parse为true,当成freemarker的模板文件
http://freemarker.org/docs/ref_directive_include.html
13.<#if>,<#list> 中的不能使用${},而是直接使用变量名称
14.boolean(true/false)变量不能直接输出,可以这样输出 ${myBool?c} 或者 ${myBool?string('yes','no')}
15.注释采用<#--和-->, jsp的注释方式不能用
16.JSON字符串的使用
<#assign dataJson = ret?eval />
ret是JSONObject,通过setAttr("ret",ret")设置过
json array长度的判断 <#if (ret.arr?size==0)> 或 ${ret.arr?size}
在#if指令中使用,要用括号括起来,否则会出错,即类似 <#if user?? && (ret.arr?size>0)>
list类型数据,也可以这样计算长度
17.在变量为null或未设置时,显示默认值
${user!""} user为null是显示空白字符串
${(user.name)!""} user或user.name为null时显示空白字符串
在#if指令上也可以用类似用法,<#if (user.name)??> 表示user或者user.name为null
18.设置自增变量
<#assign i = 1 />
<#assign i = i+1 />
相关文章推荐
- Java自定义注解Annotation
- iOS 发布上传 Error ITMS-90046
- 成长记-UILable设置行间距
- Spring MVC中设置编码格式为utf-8的方式
- Entity Framework6 with Oracle(可实现code first)
- php单链表实现代码分享
- iOS折线图实现(一)
- java组合模式(Composite)-结构性
- msdn webcast 下载地址整理
- mysql 锁超时定位与分析
- jdbc 批量操作数据库(插入、更新)
- Mysql学习笔记
- linux portmap和nfs
- 解决ListView setOnItemLongClickListener 长按无响应(嵌套GridView)
- python iter( )函数
- jsp中的${}
- 文件上传
- 图片预览,格式确认
- CoreText
- R生成颜色PDF