简体   繁体   English

如何在freemarker中检查空列表

[英]How to check for null List in freemarker

Say my java code has List<String> listS =null and i pass this to my template file. 假设我的java代码有List<String> listS =null ,我把它传递给我的模板文件。

Now i want to make sure that if list has some data then only do something . 现在我想确保if list has some data then only do something

I have tried 我试过了

<#if listS = null>
AND
<#if !listS>
AND
<#if listS?size=0>

But none of these seem to be working. 但这些似乎都没有奏效。 I have some logic i my java code ; 我的java代码有一些逻辑; through which , if some condition is true, then i new this listS and populate it. 通过它,如果某些条件为真,那么我newlistS和填充它。

Hence i need to know if the listS has been populated or is null only, in my template file. 因此,我需要知道listS是否已填充或仅在我的模板文件中为空。

How do i do this? 我该怎么做呢? Thanks. 谢谢。

EDIT: Also, i have a list of Structures, each containing this listS,(populated or not is a different issue), and i am passing the entire list of structure, hence passing a boolean value to the template file along with my list of Structures is not possible, since i will have to traverse within each list, and that traversal I want to do in the template file itself. 编辑:此外,我有一个结构列表,每个包含这个列表,(填充或不是一个不同的问题),我传递整个结构列表,因此传递一个布尔值与模板文件以及我的列表结构是不可能的,因为我将不得不在每个列表中遍历,并且我想在模板文件本身中进行遍历。

EDIT 2: For those who know what's Java null, FreeMarker 2.3.x treats them as missing values. 编辑2:对于那些知道什么是Java null的人,FreeMarker 2.3.x将它们视为缺失值。 Simply, the template language doesn't know the concept of null. 简单地说,模板语言不知道null的概念。 For example, if you have a bean that has a maidenName property, and the value of that property is null, then that's the same as if there were no such property at all, as far as the template is concerned (assuming you didn't configured FreeMarker to use some extreme object wrapper, that is). 例如,如果你有一个具有maidenName属性的bean,并且该属性的值为null,那么就模板而言,就像没有这样的属性一样(假设你没有)配置FreeMarker使用一些极端的对象包装器,即)。 The result of a method call that returns null is also treated as a missing variable (again, assuming that you use some usual object wrapper). 返回null的方法调用的结果也被视为缺失变量(同样,假设您使用了一些常用的对象包装器)。 See more in the FAQ. 请参阅常见问题解答。

Freemarker Manual Freemarker手册

But I still havent got the answer for how to make it work, if at all I can. 但是我仍然没有得到如何让它工作的答案,如果可以的话。

使用内置的has_content

<#if list5?has_content>

You can also use the missing value test operator , as such: 您还可以使用缺失值测试运算符 ,如下所示:

<#if listS??>
    <#list listS.stuff as stuff>
        ${stuff.value}
    </#list>
</#if>
<#list myList![] as element>
   <#--  do something per element -->
</#list>

If the list is null or empty the result wil be empty. 如果列表为null或为空,则结果为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM