从freemarker列表中获取最后一条记录

时间:2011-05-04 06:37:23

标签: java spring-mvc freemarker

    <select name="showYears">
            <#list payrollYears as year> 
                <option value="${year.year}">${year.yeardesc}</option>
            </#list>        
    </select>

我从我的控制器获得payrollyears列表,我在freemarker中迭代列表,并在选择框中添加值我希望我的列表的最后一个值应该在最后选择值我该怎么做

3 个答案:

答案 0 :(得分:8)

您可以执行类似

的操作
<#list payrollYears as year> 
      <option value="${year.year}" <#if !(year_has_next)>selected</#if> >${year.yeardesc}</option>
</#list> 

答案 1 :(得分:3)

对于FreeMarker 2.3.24,您可以执行类似year?has_next而非year_has_next的操作。

  • item_has_nextitem?has_next弃用):布尔值,用于指示当前项目是否为序列中的最后一项。

请参阅FreeMarker Docs

答案 2 :(得分:1)

<#list body.result as school_names_list>
{
  "NAME": <#if school_names_list.NAME??>"${school_names_list.NAME}"<#else>""</#if>,
  "ADDRESS": <#if school_names_list.ADDRESS??>"${school_names_list.ADDRESS}"<#else>""</#if>,
   <#if school_names_list?is_last><#else>,</#if>
</#list>


//Here **school_names_list** is a list and we check the last element though **school_names_list?is_last** (where list name is school_names_list)

//In this example, if it the last element, ***we'll avoid adding "," else we add "," as per JSON rules of a list.***