原文网址:FreeMarker系列--list的用法(长度,遍历,下标,嵌套,排序)_IT利刃出鞘的博客-CSDN博客
简介
本文介绍FreeMarker的list的用法。
大小
Java
java
ArrayList<String> list = new ArrayList<String>();
Freemaker
java
${list?size}
遍历list
java
<#list animals as being>
<tr>
<td>${being.name}${being.price}<td>
</tr>
</#list>
遍历MAP
html
<#list map?keys as k>
<option value="${k}">${map[k]}</option>
</#list>
下标序号
_index是list的一个属性
html
<#list list as a>
${a_index}
</#list>
第i个元素的值
html
${list[i]}
嵌套时前面要有括号,如下,将字符串变成list,然后取第i个元素的值
html
${(str?split(","))[i]}
嵌套时前面要有括号,如下,将字符串变成list,然后取第i个元素的值
html
${(str?split(","))[i]}
list的嵌套
html
<#list jsskList as jsskVO>
<#list kcList as kcVO>
<#if kcVO.kch=jsskVO.kch> (kcVO里有编号和名称,而jsskVO里只有编号)
${kcVO.kcm}
</#if>
</#list>
</#list>
list排序
升序 .sort_by()
html
<#list list?sort_by("字段") as x>
</#list>
降序 .sort_by()?reverse
html
<#list list?sort_by("字段")?reverse as x>
</#list>
item_has_next,size
html
<#list userList as user>
<#if !user_has_next>
共有${userList?size}最后一个用户是:${user.userName}
</#if>
</#list>