`
awaitdeng
  • 浏览: 214001 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

freemarker笔记 空值 及数据处理

阅读更多
最近学习了一下Freemarker对于大型门户来说,日访问量如果上万的话,首页内容比较多的话,优化方法除了采用连接池、Css Sprite、Js压缩之外,非常有必要对首页进行静态化的处理,使用Struts2就会接触到Freemarker这个非常好用的静态化工具,搞了半天终于把现在进行的首页进行全静态化。下次再把Freemarker的源码通读一遍
Freemarker学习笔记:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1) 空值Null配置处理
a.通过Configuration设置。Configuration cfg = new Configuration(); cfg.setClassicCompatible(true);//设置属性
b.通过Eviroment设置。
   Environment env = template.createProcessingEnvironment(root, out);
   env.setClassicCompatible(true);
c.通过ftl设置:在ftl前加入<!--#setting classic_compatible=true-->
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2) 空值Null判断
${(category.parentCategory.id)!0}   加括号代表category, category.parentCategory, category.parentCategory.id这三个都会进行判断;  不加括号只判断category.parentCategory.id是否为null
<#if list?exists && list.size != 0 >
空值判断:${book.name?if_exists },
${book.name?default(‘xxx’)}//默认值xxx
${ book.name!"xxx"}//默认值xxx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3) 时间格式化显示
<#assign datetimeformat="yyyy-MM-dd HH:mm:ss zzz">
<lastBuildDate>${date?string(datetimeformat)}</lastBuildDate>
<pubDate>${date?string(datetimeformat)}</pubDate>

数字格式:${book?string.number}--20
${book?string.currency}--<#-- $20.00 -->
${book?string.percent}—<#-- 20% -->
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4) 字符串截取substring,注意substring中括号的位置
<#if res1.NAME?length<12>
${res1.NAME}
<#else>
${res1.NAME?substring(0,12)}
</#if>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5) String内置的JavaScript转换: js_string
用途:用于JavaScript转义,转换',",换行等特殊字符
模板:
<script>
alert("${errorMessage?js_string}");
</script>
输出:
<script>
alert("Readonly\'s pet name is \"Cross Bone\"");
</script>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6) 内置的默认值处理:default
用途: 用于处理默认值
模本:
User: ${userLogin.name?default("Anonymous")}
<td>${(employee.department.manager.name)?default(" ")}</td>

输出:
User: Anonymous
<td> </td>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7) Sequence内置的计数器: xxx_index
用途:显示序号
模板:
<#list employees as e>
${e_index}. ${e.name}
</#list>

输出:
1. Readonly
2. Robbin
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8) 插入布尔值:
<#assign foo=ture />
${foo?string("yes","no")} <#-- yes -->
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9) 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>  // 不够4个的以“-”填充
<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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics