freemarker循环、下标及判断

本文介绍了Freemarker模板引擎中列表循环的基本用法,包括使用<#list>指令遍历列表、获取当前迭代项的位置等,并通过具体示例展示了如何在实际项目中应用这些知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、freemarker中list循环使用非常频繁,下面介绍lfreemarker中list简单的用法

  1、在freemarker中遍历list数组使用list指令:<#list sequence as item>...</#list>;

  其中sequence为集合(controller)的表达式,item是循环变量名(别名),不可是表达式;

  在遍历sequence的时候会将sequence中的变量或者对象放到item中,后面使用时只需用item即可;

  例如:<#list libraryVOs as libraryVO>

  注: libraryVOs集合为标签集合,GroupDTO为标签组 

  libraryVOs为sequence表达式,libraryVO为item变量;libraryVOS中有多个GroupDTO对象,在遍历时会将GroupDTO存放到libraryVO中。如果取GroupDTO中的名称

  使用${libraryVO.applyName};

  2、item_index:当前迭代项在所有迭代项中的位置,是数字值。

  3、freemarker判断<#if (x>y)></#if> 注:要使用括号括起来

  

<ul>
    <#list libraryVOs as libraryVO>
        <li>
            <span class="content-l-title">${libraryVO.groupDto.applyName}(${libraryVO.count}):</span>
            <ul class="content-l-content">
                <#list libraryVO.labelDto as labelLibrary>
                    <#if (labelLibrary_index <= 3)>
                        <li>
                            <span class="second-title">${labelLibrary.applyName}:</span>
                            <span class='second-title hide' id='labelId${labelLibrary.autoId}'>${labelLibrary.applyName}</span>
                            <label for="checkbox10" class="checkbox${labelLibrary.autoId}0" onclick="getLabelCustom(${labelLibrary.autoId})">
                                <input type="radio" id="checkbox0${labelLibrary.autoId}" value="1" name="tag1">
                                <b><img src="${resRoot}/css/images/xianze.png" alt=""></b>
                                    自定义
                            </label>
                            <label for="checkbox11" class="checkbox${labelLibrary.autoId}1" onclick="getLabelAll(${labelLibrary.autoId})">
                                <input type="radio" id="checkbox1${labelLibrary.autoId}" value="0" name="tag1">
                                <b><img src="${resRoot}/css/images/xianze.png" alt=""></b>
                                    不限
                            </label>
                            <input id="tagSel${labelLibrary.autoId}" name="tagSel${labelLibrary.autoId}" onclick="showTag(${labelLibrary.autoId}); return false;" class="tag-item ${labelLibrary.autoId} hide" type="text" readonly value="">
                            <input id="tagSelId${labelLibrary.autoId}" name="tagSelId${labelLibrary.autoId}" type="hidden" value="" style="width:120px;">
                            <div id="tagContent${labelLibrary.autoId}" class="tagContent${labelLibrary.autoId}" style="position: absolute; left: 64px; display:none;width:160px;height:200px;overflow-y:scroll;overflow-x:auto;z-index:5555;background: #fff;">
                                <ul id="tagDemo${labelLibrary.autoId}" class="ztree"></ul>
                            </div>
                        </li>
                    </#if>
                    
                    <#if (labelLibrary_index > 3)>
                        <li class="list hide">
                             <span class="second-title">${labelLibrary.applyName}:</span>
                            <span class='second-title hide' id='labelId${labelLibrary.autoId}'>${labelLibrary.applyName}</span>
                            <label for="checkbox10" class="checkbox${labelLibrary.autoId}0" onclick="getLabelCustom(${labelLibrary.autoId})">
                                <input type="radio" id="checkbox0${labelLibrary.autoId}" value="1" name="tag1">
                                <b><img src="${resRoot}/css/images/xianze.png" alt=""></b>
                                    自定义
                            </label>
                            <label for="checkbox11" class="checkbox${labelLibrary.autoId}1" onclick="getLabelAll(${labelLibrary.autoId})">
                                <input type="radio" id="checkbox1${labelLibrary.autoId}" value="0" name="tag1">
                                <b><img src="${resRoot}/css/images/xianze.png" alt=""></b>
                                    不限
                            </label>
                            <input id="tagSel${labelLibrary.autoId}" name="tagSel${labelLibrary.autoId}" onclick="showTag(${labelLibrary.autoId}); return false;" class="tag-item ${labelLibrary.autoId} hide" type="text" readonly value="">
                            <input id="tagSelId${labelLibrary.autoId}" name="tagSelId" type="hidden" value="" style="width:120px;">
                            <div id="tagContent${labelLibrary.autoId}" class="tagContent${labelLibrary.autoId}" style="position: absolute; left: 64px; display:none;width:160px;height:200px;overflow-y:scroll;overflow-x:auto;z-index:5555;background: #fff;">
                                <ul id="tagDemo${labelLibrary.autoId}" class="ztree"></ul>
                            </div>
                        </li>
                    </#if>
                 </#list>
                 <#if (libraryVO.labelDto?size > 3)>
                     <li>
                        <span class="l-more">更多<i class="more-icon"></i></span>
                        <span class="l-close hide">收起<i class="close-icon"></i></span>
                    </li>
                </#if>
            </ul>
        </li>
    </#list>        
</ul>

 

  

  

  

 

转载于:https://www.cnblogs.com/seedling/p/9204752.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值