This is a simple tools class to allow easy access to static fields in a class, such as string constants from within a template. Velocity will not introspect for class fields (and won't in the future :), but writing setter/getter methods to do this is a pain, so use this if you really have to access fields.
Example uses in a template: ## here we access a constant in a class include in the configuration $field.COUNTER_NAME ## here we dynamically lookup a class' fields to find another constant $field.in("org.com.SomeClass").ANOTHER_CONSTANT ## here we pass an object instance in (an Integer in this case) and ## retrieve a static constant from that instance's class $field.in(0).MIN_VALUE ## by default, once we've searched a class' fields, those fields stay ## available in the tool (change this by storeDynamicLookups="false") ## so here we get another constant from the Integer class $field.MAX_VALUE Example tools.xml config: <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.FieldTool" include="org.apache.velocity.runtime.RuntimeConstants,com.org.MyConstants"/> </toolbox> </tools>
Right now, this tool only gives access to public static
fields. It seems that anything else is too dangerous. This is for convenient access to 'constants'. If you have fields that aren't static
, handle them by explicitly placing them into the context or writing a getter method.
@author Geir Magnusson Jr.
@author Nathan Bubna
@since VelocityTools 2.0
@version $Id: FieldTool.java 463298 2006-10-12 16:10:32Z henning $
|
|
|
|
|
|