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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5879492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:15:33  来源:igfitidea点击:

get last record from list in freemarker

javaspring-mvcfreemarker

提问by Anil

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

i am getting payrollyears list from my controller and i am iterating the list in freemarker and adding value to select box i want my last value of list should be selected value in last how can i do that

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

回答by Dirk

You could do something like

你可以做类似的事情

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

回答by jpllosa

For FreeMarker 2.3.24, you can do something like year?has_nextinstead of year_has_next.

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

  • item_has_next(deprecated by item?has_next): Boolean value that tells if the current item is the last in the sequence or not.
  • item_has_next(已弃用item?has_next):布尔值,指示当前项目是否是序列中的最后一个。

See FreeMarker Docs

查看FreeMarker 文档

回答by Vibhu Garg

<#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.***