___________________________________________________________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>