Velocity If Statement is Type Sensitive
I discovered that comparisons in Velocity if statements are type-sensitive the hard way. This can cause subtle bugs that are difficult to track down, especially when working with HTML form data.
The Problem
I was trying to display the 'selected' option in an HTML <SELECT>
object. The list of possible values is $list
, each is a simple 'NameValuePair' value object { String name, String value }
. The object I am editing in this form is $obj
, and the field I am testing against is $obj.typeId
defined as a long.
#foreach ( $item in $list )
#if ( $item.value == $obj.typeId )
<option value="$item.value" selected>$item.name</option>
#else
<option value="$item.value">$item.name</option>
#end
#end
However, the if statement on line 2 is actually comparing a String to a long which are of course never going to be equal.
The Solution
The easiest work around in this case was to convert the $obj.typeId
into a String before the comparison.
#set ( $typeId = "$!{obj.typeId}" )
#foreach ( $item in $list )
#if ( $item.value == $typeId )
<option value="$item.value" selected>$item.name</option>
#else
<option value="$item.value">$item.name</option>
#end
#end
The $!{obj.typeId}
syntax ensures that the long value is converted to a string representation that can be properly compared with the string values from the list.
Key Takeaway
Always be mindful of data types when performing comparisons in Velocity templates. Unlike some other template engines, Velocity doesn't perform automatic type coercion, so explicit type conversion is necessary when comparing values of different types.
Any comments / feedback welcomed ;)
Related Posts
Claude Code Rebuilt My Website in 25 Minutes for $8
I gave Claude Code an XML backup of my 19-year-old WordPress blog and asked it to rebuild everything as a modern NextJS site. What happened next was like watching a swarm of expert developers work in parallel—spawning agents, debugging TypeScript errors, and shipping production-ready code. All in 26 minutes. For eight dollars.
Analysis of Spamming Zombie Botnets
Deep technical analysis of how comment spam evolved from simple bots to sophisticated coordinated botnets, including real attack logs and countermeasures
Velocimacro - Generate a HTML Select box and its Options
A quick and simple Velocimacro to generate HTML Select boxes and its Options; automatically selects the correct option based on the value given.