Velocity object types

Basic types

String

Each string value is stored internally in a "String" Java object ( see "java.lang.String" for details ).
Initialization :
1
#set( $str = "abc" )
2
#set( $r = "xx${str}yy" ) ## r : "xxabcyy"
3
#set( $r = "xx" + $str + "yy" ) ## r : "xxabcyy"
Copied!
String methods usage examples :
1
length : $str.length()
2
#set($str2 = $str.replaceFirst("def", "xy" ) )
3
#if ( $str.endsWith("ef") ) YES #end
4
#if ( $str.equalsIgnoreCase("AbC") ) YES #end
5
toUpperCase() : $str.toUpperCase()
6
toLowerCase() : $str.toLowerCase()
Copied!

Integer

Each integer value is stored internally in a "Integer" Java object ( see "java.lang.Integer" for details ).
Initialization :
1
#set( $i = 123 )
Copied!
Calculations :
1
#set( $r = $i + 1000 ) ## r = 1123
2
#set( $r = $i - 23 ) ## r = 100
3
#set( $r = $i * 10 ) ## r = 1230
4
#set( $r = $i / 2 ) ## NB : r = 61 (!) (int)
5
#set( $r = $i / 2.0 ) ## r = 61.5
6
#set( $r = $i.doubleValue() / 2 ) ## r = 61.5
Copied!

Double

Each double value is stored internally in a "Double" Java object ( see "java.lang.Double" for details ).
Initialization :
1
#set( $d = 123.45 )
Copied!

Boolean

Each boolean value is stored internally in a "Boolean" Java object ( see "java.lang.Boolean" for details ).
Initialization :
1
#set( $b = true )
2
#set( $b = false )
3
#set( $b = $mylist.isEmpty() )
Copied!

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 :
1
## 4 numbers from 1 to 4 (with range operator)
2
#set( $mylist = [1..4])
3
4
## void list
5
#set($mylist = [])
6
7
## 6 values with diferent types
8
#set( $mylist = [1, 2, 3, "A", true, 65.78] )
Copied!
Print content :
1
mylist content : $mylist
2
3
--- OUTPUT :
4
mylist content : [1, 2, 3, A, true, 65.78]
Copied!
Print all items with "#foreach" ( "$foreach.count" goes from 1 to length, it's a count not an index ) :
1
#foreach ( $item in $mylist )
2
- $foreach.count : $item
3
#end
4
5
--- OUTPUT :
6
1 : item = 1
7
2 : item = 2
8
3 : item = 3
9
4 : item = A
10
5 : item = true
11
6 : item = 65.78
Copied!
Print a single item by index (the index goes from 0 to length-1) :
1
item 0 : $mylist[0]
2
--- OUTPUT :
3
item 0 : 1
4
5
$list.get(0) ## with Java get() method
6
--- OUTPUT :
7
item 0 : 1
8
9
item 3 : $mylist[3]
10
--- OUTPUT :
11
item 3 : A
12
13
item 6 : $mylist[6]
14
--- ERROR :
15
Index 6 out of bounds for length 6
Copied!
Change list content :
1
#set( $mylist[2] = "CCC" ) ## index starts at 0
2
3
#set( $z = $mylist.add("new item") )
4
#set( $z = $mylist.remove("A") )
5
#set( $z = $mylist.removeAll( [1, 2, true, "Y"] ) )
6
## 'set($z=..)' is a work around to avoid printing
7
## the return value from 'add', 'remove', etc
Copied!
Other examples :
1
isEmpty : $mylist.isEmpty()
2
#if ( $mylist.isEmpty() ) list is void #end
3
4
size : $mylist.size()
5
#if ( $mylist.size() > 0 ) list is not void #end
6
7
$mylist.indexOf("A") ## OUTPUT : 3 (found at index 3)
8
$mylist.indexOf("Z") ## OUTPUT : -1 (not found)
9
10
$mylist.contains("A") ## OUTPUT : true (found)
11
$mylist.contains("Z") ## OUTPUT : false (not found)
12
13
$mylist.subList(1,3) ## from 1 to 2 (3 is exclusive)
Copied!

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 :
1
#set( $mymap = {"k1" : "v1" , "k2" : "v2"} )
Copied!
Print content :
1
mymap content : $mymap
2
--- OUTPUT :
3
mymap content : {k1=v1, k2=v2}
4
5
## print VALUES
6
#foreach($v in $mymap )
7
. $v
8
#end
9
10
## print KEYS with Java "keySet()"
11
#foreach($k in $mymap.keySet() )
12
. $k
13
#end
14
15
## print KEYS and VALUES with Java "entrySet()"
16
#foreach($e in $mymap.entrySet() )
17
. $e.key : $e.value
18
#end
Copied!
Get by key:
1
k1 : $mymap["k1"]
2
k2 : $mymap["k2"]
3
k3 : $mymap["k3"] ## error (no key "k3")
Copied!
Set by key ( add or update a key-value pair ) :
1
#set( $mymap["k0"] = "v0" )
Copied!
Other examples using the Java map methods :
1
isEmpty : $mymap.isEmpty()
2
#if ( $mymap.isEmpty() ) void #end
3
4
size : $mymap.size()
5
#if ( $mymap.size() > 0 ) not void #end
6
7
#if ( $mymap.containsKey("k1") ) K1 FOUND #end
8
9
#if ( $mymap.containsValue("v2") ) V2 FOUND #end
10
11
## Remove element by key
12
#set($removed = $mymap.remove("k1") )
13
#if ( ! $mymap.containsKey("k1") ) K1 NOT FOUND #end
14
15
## Get all values
16
#foreach($v in $mymap.values() )
17
. $v
18
#end
19
20
## Get all keys
21
#foreach($key in $mymap.keySet() )
22
. $key
23
#end
Copied!

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 :
1
#set( $array = $mylist.toArray() )
Copied!
Print all items with "#foreach" ( "$foreach.count" goes from 1 to length, it's a count not an index ) :
1
#foreach ( $item in $array)
2
$foreach.count : $item
3
#end
4
5
--- OUTPUT :
6
1 : item = A
7
2 : item = B
8
3 : item = C
9
4 : item = D
Copied!
1
#set($last = $array.size() - 1 ) ## last index
2
#foreach ( $i in [0..$last] ) ## index 'range'
3
// index : $i --> element : $array[$i]
4
#end
5
6
--- OUTPUT :
7
// index : 0 --> element : A
8
// index : 1 --> element : B
9
// index : 2 --> element : C
10
// index : 3 --> element : D
Copied!
Change value :
1
#set( $array[2] = "newValue" ) ## index starts at 0
Copied!
Other examples :
1
isEmpty : $array.isEmpty()
2
#if ( $array.isEmpty() ) is void #end
3
4
size : $array.size()
5
#if ( $array.size() > 0 ) is not void #end
6
7
$array.contains("B") ## OUTPUT : true (found)
8
$array.indexOf("B") ## OUTPUT : 1 (found at index 1)
Copied!
Last modified 7mo ago