• Velocity will properly 'narrow' args to method calls, so calling setFoo( int i ) with an int placed into the context via
#set() will work fine.
Other Context Issues
One of the features provided by the VelocityContext (or any Context derived from AbstractContext) is node specific introspection
caching. Generally, you as a the developer don't need to worry about this when using the VelocityContext as your context.
However, there is currently one known usage pattern where you must be aware of this feature.
The VelocityContext will accumulate intropection information about the syntax nodes in a template as it visits those nodes. So, in
the following situation:
• You are iterating over the same template using the same VelocityContext object.
• Template caching is off.
• You request the Template from getTemplate() on each iteration.
It is possible that your VelocityContext will appear to 'leak' memory (it is really just gathering more introspection information.)
What happens is that it accumulates template node introspection information for each template it visits, and as template caching is
off, it appears to the VelocityContext that it is visiting a new template each time. Hence it gathers more introspection information
and grows. It is highly recommended that you do one or more of the following:
• Create a new VelocityContext for each excursion down through the template render process. This will prevent the
accumulation of introspection cache data. For the case where you want to reuse the VelocityContext because it's populated
with data or objects, you can simply wrap the populated VelocityContext in another, and the 'outer' one will accumulate the
introspection information, which you will just discard. Ex. VelocityContext useThis = new VelocityContext
( populatedVC );This works because the outer context will store the introspection cache data, and get any requested data
from the inner context (as it is empty.) Be careful though -if your template places data into the context and it's expected
that it will be used in the subsequent iterations, you will need to do one of the other fixes, as any template #set() statements
will be stored in the outermost context. See the discussion inContext chaining for more information.
• Turn on template caching. This will prevent the template from being re-parsed on each iteration, resulting the the
VelocityContext being able to not only avoid adding to the introspection cache information, but be able to use it resulting in
a performance improvement.
• Reuse the Template object for the duration of the loop iterations. Then you won't be forcing Velocity, if the cache is turned
off, to reread and reparse the same template over and over, so the VelocityContext won't gather new introspection
information each time.
Using Velocity
If you are using VelocityViewServlet or other web frameworks, you may never call Velocity directly. However, if you use
Velocity for non-web purposes, or create your own web framework you will need to directly call the Velocity Engine similar
tothe fundamental pattern shown earlier. One important additional thing to remember is to initialize the Velocity Engine before
using it to merge templates.
The Velocity Helper Class
Velocity contains an application utility class called Velocity ( org.apache.velocity.app.Velocity ). The purpose of this class
is to provide the necessary methods required to initialize Velocity, as well as useful utility routines to make life easier in using
Velocity. This class is documented in the project's javadoc, so please look there for definitive details. This documentation is
intended to be of a tutorial nature; therefore for compete API information, the Javadoc is the definitive source.
The Velocity runtime engine is a singleton instance that provides resource, logging and other services to all Velocity users
running in the same JVM. Therefore, the runtime engine is initialized only once. You can attempt to initialize Velocity more than
once, but only the first initialization will apply. The rest of the attempts will be ignored. The Velocity utility class currently
provides five methods used in configuration of the runtime engine.
The five configuration methods are:
• setProperty( String key, Object o )
Sets the property key with the value o. The value is typically a String, but in special cases can also be a comma-separated
list of values (in a single String, ex."foo, bar, woogie") as well as other things that will arise.
• Object getProperty( String key )
Returns the value of the property key. Note that you must be aware of the type of the return value, as they can be things
other than Strings.