Velocity object types

Basic types

String

Each string value is stored internally in a "String" Java object ( see "java.lang.String" for details ).

Initialization :

#set( $str = "abc" )
#set( $r = "xx${str}yy" )       ## r : "xxabcyy"
#set( $r = "xx" + $str + "yy" ) ## r : "xxabcyy"

String methods usage examples :

length :  $str.length() 
#set($str2 = $str.replaceFirst("def", "xy" ) )
#if ( $str.endsWith("ef") ) YES #end
#if ( $str.equalsIgnoreCase("AbC") ) YES #end
toUpperCase() : $str.toUpperCase()
toLowerCase() : $str.toLowerCase()

Integer

Each integer value is stored internally in a "Integer" Java object ( see "java.lang.Integer" for details ).

Initialization :

Calculations :

Double

Each double value is stored internally in a "Double" Java object ( see "java.lang.Double" for details ).

Initialization :

Boolean

Each boolean value is stored internally in a "Boolean" Java object ( see "java.lang.Boolean" for details ).

Initialization :

Collections

List

Object containing a list of any type of values. The values are stored internally in a "ArrayList" Java object ( see java.util.ArrayList for details ).

Even if it's not a real "Java array", this object is often considered as an "array" in many Velocity documentations..

Initialization :

Print content :

Print all items with "#foreach" ( "$foreach.count" goes from 1 to length, it's a count not an index ) :

Print a single item by index (the index goes from 0 to length-1) :

Change list content :

Other examples :

Map

Object containing Key-Value associations. The "key-value" pairs are stored internally in a "LinkedHashMap" Java object (see "java.util.LinkedHashMap" for details)

Initialization :

Print content :

Get value by key (if the key doesn't exist in the map the error " : no attribute '[' " occurs)

Get value by key with default value if key not in map (secure, no error):

Each map key is also usable as an object's property then it's possible to get its value using the "object dot notation":

Set an entry in the map (add or update a key-value pair):

Remove an entry by key (error if the key doesn't exist in the map):

Remove an entry by key and value (secure, no error if no match):

Other examples using the Java map methods :

Array

"Pure array" object that can be obtained from other objects.

The values are stored internally in a "Object [ ] " instance. And therefore it doesn't have all the features offered by a List. An array does not support operations changing its size ( add, remove, removeAll, etc)

Initialization :

Print all items with "#foreach" ( "$foreach.count" goes from 1 to length, it's a count not an index ) :

Change value :

Other examples :

Last updated