Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Now i want to make sure that 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 ; through which , if some condition is true, then i new this listS and populate it.

Hence i need to know if the listS has been populated or is null only, in my template file.

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. Simply, the template language doesn't know the concept of 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). 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). See more in the FAQ.

Freemarker Manual

But I still havent got the answer for how to make it work, if at all I can.

like image 265
Kraken Avatar asked Sep 24 '12 12:09

Kraken


People also ask

How do you check for null in FreeMarker?

The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function is often used with another built-in FreeMarker function to detect for null values.

What is eval in FreeMarker?

eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)

How do you use the ternary operator in FreeMarker?

When applied to a boolean, the string built-in will act as a ternary operator. It's no very readable as this is not the intended usage of it. It's for formatting boolean values, like Registered: ${registered? string('yes', 'no')} .

How do you comment out code on FreeMarker?

Comments: <#-- and --> Comments are similar to HTML comments, but they are delimited by <#-- and -->. Comments will be ignored by FreeMarker, and will not be written to the output.


2 Answers

Use the has_content built-in:

<#if list5?has_content> 
like image 87
Bohemian Avatar answered Sep 20 '22 15:09

Bohemian


You can also use the missing value test operator, as such:

<#if listS??>     <#list listS.stuff as stuff>         ${stuff.value}     </#list> </#if> 
like image 27
Tarek Avatar answered Sep 19 '22 15:09

Tarek