Strict References Setting - Precisely_EnterWorks - EnterWorks - 11.0

EnterWorks Guide

Product type
Software
Portfolio
Verify
Product family
EnterWorks
Product
Precisely EnterWorks
Precisely EnterWorks > EnterWorks
Version
11.0
Language
English
Product name
Precisely EnterWorks
Title
EnterWorks Guide
Copyright
2024
First publish date
2007
Last updated
2025-01-07
Published on
2025-01-07T07:44:20.997352

Velocity 1.6 introduces the concept of strict references, which is activated by setting the velocity configuration property 'runtime.references.strict' to true. With this setting references are required to be either placed explicitly into the context or defined with a #set directive, or Velocity will throw an exception. References that are in the context with a value of null will not produce an exception. Additionally, if an attempt is made to call a method or a property on an object within a reference that does not define the specified method or property then Velocity will throw an exception. This is also true if there is an attempt to call a method or property on a null value.

In the following examples $bar is defined, but $foo is not, and all these statements will throw an exception:

$foo ## Exception
#set($bar = $foo) ## Exception
#if($foo == $bar)#end ## Exception
#foreach($item in $foo)#end ## Exception

Also, the following statements show examples in which Velocity will throw an exception when attempting to call methods or properties that do not exist. In these examples $bar contains an object that defines a property 'foo' which returns a string, and 'retnull' which returns null.

$bar.bogus  ## $bar does not provide property bogus, Exception
$bar.foo.bogus  ## $bar.foo does not provide property bogus, Exception
$bar.retnull.bogus   ## cannot call a property on null, Exception

In general, strict reference behavior is true for all situations in which references are used, except for a special case within the #if directive. If a reference is used within a #if or #elseif directive without any methods or properties, and if it is not being compared to another value, then undefined references are allowed. This behavior provides an easy way to test if a reference is defined before using it in a template. In the following example, where $foo is not defined, the statements will not throw an exception.

#if ($foo)#end ## False
#if ( ! $foo)#end ## True
#if ($foo && $foo.bar)#end    ## False and $foo.bar will not be evaluated
#if ($foo && $foo == "bar")#end   ## False and $foo == "bar" will not be evaluated
#if ($foo1 || $foo2)#end   ## False $foo1 and $foo2 are not defined
Note: Undefined macro references will also throw an exception with the strict reference setting.