经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTMLCSS » CSS » 查看文章
在HTML代码中使用freemarker
来源:cnblogs  作者:GJQUser  时间:2018/10/8 8:47:11  对本文有异议

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

 

 

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>

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号