http://wiki.apache.org/velocity/VelocityFAQ:
Q: How can i access my object's public fields in my templates?
A: Currently, you have three options:
Wrap your object with a FieldMethodizer
Configure your VelocityEngine to use a custom uberspector like the PublicFieldUberspect
Lobby the velocity-dev list to add public field introspection as a default fallback if no matching method is found :)
FieldMethodizer
works only with public static fields.
PublicFieldUberspect
example code is quite old and it just fails with error on nonexistent fields.
And forget about lobby at dev list.)
Meanwhile, there is good caching implementation of UberspectPublicFields in current velocity trunk. Unfortunately, there was no active development for years and no plans for next release are published.
One would have to build it himself and bundle in private repository.
Another altervative is a fork with bonus scala compatibility that is available in central maven repository:
https://mvnrepository.com/artifact/com.sksamuel.scalocity/scalocity/0.9.
Drop in instead of usual velocity dependency:
<dependency>
<groupId>com.sksamuel.scalocity</groupId>
<artifactId>scalocity</artifactId>
<version>0.9</version>
</dependency>
Then just add to velocity.properties
:
runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectPublicFields, org.apache.velocity.util.introspection.UberspectImpl
The caveat is that UberspectImpl
is patched with additional support for scala properties and requires 8 MB scala jar.
Eventually, I just interned the following classes from velocity trunk into own project:
org.apache.velocity.runtime.parser.node.PublicFieldExecutor
org.apache.velocity.runtime.parser.node.SetPublicFieldExecutor
org.apache.velocity.util.introspection.ClassFieldMap
org.apache.velocity.util.introspection.Introspector
org.apache.velocity.util.introspection.IntrospectorBase
org.apache.velocity.util.introspection.IntrospectorCache
org.apache.velocity.util.introspection.IntrospectorCacheImpl
org.apache.velocity.util.introspection.UberspectPublicFields
These work fine with Velocity 1.7.