Now that you are familiar with references, you can begin to apply them effectively in your templates. Velocity references take advantage of some Java principles that template designers will find easy to use. For example:
$foo
$foo.getBar()
## is the same as
$foo.Bar
$data.setUser("jon")
## is the same as
#set( $data.User = "jon" )
$data.getRequest().getServerName()
## is the same as
$data.Request.ServerName
## is the same as
${data.Request.ServerName}
These examples illustrate alternative uses for the same references. Velocity takes advantage of Java's introspection and Bean features to resolve the reference names to both objects in the Context as well as the objects methods. It is possible to embed and evaluate references almost anywhere in your template.
Velocity, which is modeled on the Bean specifications defined by Sun Microsystems, is case
sensitive; however, its developers have strived to catch and correct user errors
wherever possible. When the method getFoo()
is referred to in a
template by $bar.foo
, Velocity will first try $getfoo
. If this fails, it will then try $getFoo
. Similarly, when a template
refers to $bar.Foo
, Velocity will try $getFoo()
first,
then try getfoo()
.
References to instance variables in a template are not resolved.
Only
references to the attribute equivalents of JavaBean getter/setter methods are resolved
(i.e. $foo.Name does resolve to the class Foo's getName()
instance
method, but not to a public Name
instance variable of Foo).