Freemarker list 的簡單使用
freemarker list (長度,遍歷,下標,嵌套,排序)
1. freemarker獲取list的size :
Java
ArrayList<String> list = new ArrayList<String>();
Freemaker
${list?size}
2. list的遍歷:
<#list animals as being>
<tr>
<td>${being.name}${being.price}<td>
</tr>
</#list>
3. 遍歷MAP
<#list map?keys as k>
<option value="${k}">${map[k]}</option>
</#list>
4.list遍歷中的下標序號:
_index是list的一個屬性
<#list list as a>
${a_index}
</#list>
5.取LIST中第i個元素的值
${list[i]}
嵌套時前面要有括號,如下,將字符串變成list,然後取第i個元素的值
${(str?split(","))[i]}
6. list的嵌套:
<#list jsskList as jsskVO>
<#list kcList as kcVO>
<#if kcVO.kch=jsskVO.kch> (kcVO裏有編號和名稱,而jsskVO裏只有編號)
${kcVO.kcm}
</#if>
</#list>
</#list>
7. list排序:
升序 .sort_by()
<#list list?sort_by("字段") as x>
</#list>
降序 .sort_by()?reverse
<#list list?sort_by("字段")?reverse as x>
</#list>
8.item_has_next,size使用:
<#list userList as user>
<#if !user_has_next>
共有${userList?size}最後一個用戶是:${user.userName}
</#if>
</#list>
Freemarker list 的簡單使用