freemarker中list的使用

xiaoxiao2021-02-28  125

在模版页里用list展示列表内容,非常方便。全部用例子来说吧。 最常用的用法 : <#list users as user>       <span>${user.name}</span>       <span>${user.age}</span></br> </#list> 这里,是假设 Java类里有一个users的数组,或者Map,或者List等等,它的里面放的是user类,每个user有自己name,age属性。 最后显示的结果就是users里面所有user的姓名和年龄。 上例中的users处也可以是用现场定义的,如 <#list [1,2,3,4] as index>      <span>${index}</span> </#list> 结果显示是:1234 如果只是想要计数,也可以这样写 <#list 1..4 as index>    <span>${index}</span> </#list> 结果和上面是一样的,也是1234 如果需要显示当前循环到第几项,可以这样写 <#list ["hello","welcome","hi"] as word>     <span>${word_index+1},${word}</span></br> </#list> as 后面的那个变量,加上_index,就可以表示当前循环到第几项 结果是: 1,hello 2,welcome 3,hi 有时候,最后一项在显示的时候可能要做特殊处理,怎么判断最后一项? <#list ["hello","welcome","hi"] as word>     <span>${word}</span><#if word_has_next>,</#if></#list> as 后面的那个变量,加上_has_next,就可以判断是否最后一项 结果是: hello,welcome,hi 如果想在循环中判断到某一项时退出,可以这样做 <#list users as user>    <span>${user.name}</span>    <#if user.name == "pxx"><#break></#break>

</#list>

转发:http://blog.csdn.net/nairuohe/article/details/6292190

转载请注明原文地址: https://www.6miu.com/read-29984.html

最新回复(0)