If Else Construct with Comparison not Working In Velocity Template

1.5k Views Asked by At

I have below velocity template construct

#if($no_of_entries > 1)          
    <strong>Its True !!</strong>
#else
    <strong>Its False !!</strong>
#end

Even when $no_of_entries is greater than 1 , say 10 , it prints Its False !!

That means $no_of_entries > 1 is not working

Why the $no_of_entries > 1 condition returning false ?

I tried printing the value for $no_of_entries and it prints correct value which is > 1

EDIT :

I also tried using below code

#if( Integer.parseInt($no_of_entries) > 1)    

    <strong>Its True !!</strong>
#else

     <strong>Its False !!</strong>
#end

But it is not working and throwing below exception -

org.apache.velocity.exception.ParseErrorException: Encountered "Integer" at file.vm
Was expecting one of:
    "[" ...
    "{" ...
    "(" ...
    <WHITESPACE> ...
    <STRING_LITERAL> ...
    "true" ...
    "false" ...
    <INTEGER_LITERAL> ...
    <FLOATING_POINT_LITERAL> ...
    <IDENTIFIER> ...
    "{" ...
    <WHITESPACE> ...
1

There are 1 best solutions below

2
On

If it's a number it should work, if it's a String, parse it as Integer:

#set($Integer = 0)
#if($Integer.parseInt($no_of_entries) > 1) 

Or if it's a list use size() method

#if($no_of_entries.size() > 1)