___________________________________________________________html
___________________________________________________________json
freemarker中显示某对象使用${name}.服务器
但若是name为null,freemarker就会报错。若是须要判断对象是否为空:spa
<#if name??>.net
……excel
</#if>code
固然也能够经过设置默认值${name!''}来避免对象为空的错误。若是name为空,就以默认值(“!”后的字符)显示。xml
对象user,name为user的属性的状况,user,name都有可能为空,那么能够写成${(user.name)!''},表示user或者name为null,都显示为空。判断为空htm
<#if (user.name)??>对象
……
</#if>
__________________________________________________________
项目中用freemarker 作显示层,可能会遇到取出数据前几条,经过用freemarker 取数据用<#list root.list as row> ${row.title} <#/list> ,可是这种取法是取出全部的数据.
若是我想去第一条数据:
<#list arrayList as c>
<#if c_index == 0>
第一项的值
</#if>
</#list>
如今只想取前5条,该怎么作?代码以下:
<#assign n = list5?size /> //定义n的值为list5的大小
<#if n gt 6> //若是n大于6,页面中可能要求只显示6条 (注:gt,gte,lt,lte)
<#assign n = 6 /> //把n重定义为6
< /#if>
< #if n!=0> //防止n的值为0,也能够写成<#if n gt 0 >
< #list 0..(n-1) as i> //把前 n 条 记录赋值给 i,若是i=3,则[0,1,2]
< #assign ls5 = list5[i] /> //把list5的第i个元素赋值给ls5
< #assign isNew = list5Istrue[i] />
< tr>
< td height='25' class='z3'>.<a href='#' onclick="zw('${ls5.CIid}','905','活动展现','');">
< #if ls5.CTitle?length lt 15> //若是Ctitle的长度小于15,就
${ls5.CTitle} //就正常显示该标题
<#else> //若是大于15
${ls5.CTitle[0..15]}... //就截取前15个,并加上…
</#if>
< #if isNew="true">
< img src='/model/img/new-111.gif' width='27' height='11' border='0' />
< /#if>
< /a></td>
< /tr>
< /#list>
< /#if>
项目中应用:
[#assign n = 0] [#list cocoPersonInfo.cocoPersonResues as v] [#assign n = n + 1] <tr > <td width="66" colspan="2" >${v.worktime_begin}</td> <td width="66" colspan="2" >${v.worktime_end}</td> <td width="300" colspan="10" >${v.workplace}</td> <td width="168" colspan="4" >${v.occupation}</td> </tr> [#if n > 4] [#break] [/#if] [/#list] [#if n < 5] [#list n..4 as i] <tr > <td width="66" colspan="2" ></td> <td width="66" colspan="2" ></td> <td width="300" colspan="10" ></td> <td width="168" colspan="4" ></td> </tr> [/#list] [/#if]
效果:
_______________________________________________________________
FreeMarker两种注释
发布以后,客户端能够看到注释内容
效果图
<!-- A -->
<!-- wind123 CommentModelList -->
<#if staticThreadDetailDo.commentModelList??>
<#assign lastCommentIdx=staticThreadDetailDo.commentModelList?size-1 />
${staticThreadDetailDo.commentModelList[lastCommentIdx].body)}
</#if>
<!-- B -->
<#if staticThreadDetailDo.commentModelList??>
<#assign lastCommentIdx=staticThreadDetailDo.commentModelList?size-1 />
<#list staticThreadDetailDo.commentModelList as item>
<#if item_index=lastCommentIdx>
${(item.body)}
</#if>
</#list>
</#if>