类 FieldTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.FieldTool
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.
- 从以下版本开始:
- VelocityTools 2.0
- 版本:
- $Id: FieldTool.java 463298 2006-10-12 16:10:32Z henning $
- 作者:
- Geir Magnusson Jr., Nathan Bubna
-
嵌套类概要
嵌套类 -
字段概要
字段修饰符和类型字段说明static final StringThe key used for specifying which classes should be inspected for public static methods to be made available.protected HashMapstatic final StringThe key used for specifying whether or not the tool should store fields in classes dynamically looked up from within a template.protected boolean从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected voidconfigure(ValueParser values) Does the actual configuration.Returns the value for the specified field name as found in the storedMapof field names to values (or placeholders).Returns aFieldTool.FieldToolSubholding aMapof all the public static field names to values (or a placeholder if the value is not final) for the specifiedClass.Returns aFieldTool.FieldToolSubholding aMapof all the public static field names to values (or a placeholder if the value is not final) for theClassof the specified Object.Returns aFieldTool.FieldToolSubholding aMapof all the public static field names to values (or a placeholder if the value is not final) for the specified class(name).Looks for all public, static fields in the specified class and stores their value (if final) or else aFieldTool.MutableFieldfor in aMapunder the fields' names.protected Object从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
INCLUDE_KEY
The key used for specifying which classes should be inspected for public static methods to be made available.- 另请参阅:
-
STORE_DYNAMIC_KEY
The key used for specifying whether or not the tool should store fields in classes dynamically looked up from within a template. The default value is true.- 另请参阅:
-
storage
-
storeDynamicLookups
protected boolean storeDynamicLookups
-
-
构造器详细资料
-
FieldTool
public FieldTool()
-
-
方法详细资料
-
configure
从类复制的说明:SafeConfigDoes the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- 覆盖:
configure在类中SafeConfig- 参数:
values- configuration values
-
get
Returns the value for the specified field name as found in the storedMapof field names to values (or placeholders). Returnsnullif there is no matching field.- 参数:
name- field name- 返回:
- field value
-
in
Returns aFieldTool.FieldToolSubholding aMapof all the public static field names to values (or a placeholder if the value is not final) for the specified class(name). If theClasswith the specified name cannot be loaded, this will returnnull, rather than throw an exception.- 参数:
classname- target class name- 返回:
FieldTool.FieldToolSubobject- 另请参阅:
-
in
Returns aFieldTool.FieldToolSubholding aMapof all the public static field names to values (or a placeholder if the value is not final) for theClassof the specified Object.- 参数:
instance- target instance- 返回:
FieldTool.FieldToolSubobject- 另请参阅:
-
in
Returns aFieldTool.FieldToolSubholding aMapof all the public static field names to values (or a placeholder if the value is not final) for the specifiedClass.- 参数:
clazz- target class- 返回:
FieldTool.FieldToolSubobject
-
inspect
Looks for all public, static fields in the specified class and stores their value (if final) or else aFieldTool.MutableFieldfor in aMapunder the fields' names. This will never return null, only an empty Map if there are no public static fields.- 参数:
clazz- target class- 返回:
- fields map
-
retrieve
Retrieves and returns the value of the specifiedFieldin the specifiedClass. Returnsnullin case of failure.- 参数:
field- target fieldclazz- target class- 返回:
- field value
-