在HTML代碼中使用freemarker


在HTML代碼中使用freemarker

1、freemarker中顯示某對象的屬性使用${user.name}.

但如果name為null,freemarker就會報錯。如果需要判斷對象是否為空:

<#if name??>.....</#if>

當然也可以通過設置默認值${name!''}來避免對象為空的錯誤。如果name為空,就以默認值(“!”后的字符)顯示

對象user,name為user的屬性的情況,user,name都有可能為空,那么可以寫成${(user.name)!''},表示user或者name為null,都顯示為空

判斷為空:<#if (user.name)??>……</#if>

 

2、freemarker中截取字符串-------------------------------------

類型一:
<#if item.contents??>
  <#if item.contents?length gt 7>
    ${item.contents?substring(0,3)}****${item.contents?substring(7,item.contents?length)}
  <#else>${item.contents!}
  </#if>
</#if>
類型二:
<#if item.contents??>
  <#if item.contents?length gt 7>
    ${item.contents?substring(0,7)}****
  <#else>${item.contents!}
  </#if>
</#if>

<#if item.createTime??>
  <#if item.createTime?length gt 0>
    ${item.createTime?substring(0,10)}
  <#else>${item.createTime!''}
  </#if>
</#if>

<div title="${item.title!''}">${item.title?substring(0,9)}...</div>

 類型三:截取手機號的中間四位

 <#if phone??&&phone?length gt 7>${phone ? replace(phone ? substring(3,7),"****")}<#else>${phone!}</#if>

3、根據條件顯示-------------------------------------
<td class="th18">
  <#if item.type?? && item.type =="0">天
  <#elseif item.type?? && item.type =="1">**
  <#elseif item.type?? && item.type =="2">**
  <#elseif item.type?? && item.type =="3">**
  <#elseif item.type?? && item.type =="4">**
  </#if>
</td>

 

4、freemark控制class樣式-------------------------------------
<li class="biao-new <#if userConf??&&userConf.mothShortMark==1>active</#if>">

 

5、遍歷  list  或者  page

<#if list  ?? && list  ?size gt 0>
  <#list channels as item>
    <option value="${item.id!}">${item.channelName!}</option>
  </#list>
</#if>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM