类 RenderTool
Example of eval():
Input
-----
#set( $list = [1,2,3] )
#set( $object = '$list' )
#set( $method = 'size()' )
$render.eval("${object}.$method")
Output
------
3
Example of recurse():
Input
-----
#macro( say_hi )hello world!#end
#set( $foo = '#say_hi()' )
#set( $bar = '$foo' )
$render.recurse($bar)
Output
------
hello world!
Toolbox configuration:
<tools>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.generic.RenderTool">
<property name="parseDepth" type="number" value="10"/>
</tool>
</toolbox>
</tools>
Ok, so these examples are really lame. But, it seems like someone out there is always asking how to do stuff like this and we always tell them to write a tool. Now we can just tell them to use this tool.
This tool may be used in any scope, however, the context provided
for the eval(String) and recurse(String) methods
will only be current if the tool is request scoped. If application or
session scoped, then the context will be the same one set at the time
of the tool's first use. In such a case, each call to eval(String) or
recurse(String) will by default create a new Context that wraps the
configured one to prevent modifications to the configured Context
(concurrent or otherwise). If you wish to risk it and accrete changes
then you can relax the thread-safety by setting the 'forceThreadSafe'
property to 'false'.
Of course none of the previous paragraph likely applies if you are
not using the core tool management facilities or if you stick to the
eval(Context,String) and recurse(Context,String)
methods. :)
This tool by default will catch and log any exceptions thrown during rendering and instead return null in such cases. It also limits recursion, by default, to 20 cycles, to prevent infinite loops. Both settings may be configured to behave otherwise.
- 版本:
- $Revision$ $Date$
- 作者:
- Nathan Bubna
-
字段概要
字段修饰符和类型字段说明static final intThe maximum number of loops allowed when recursing.static final String已过时。static final Stringstatic final String已过时。从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected voidconfigure(ValueParser parser) Looks for deprecated parse depth and catch.exceptions properties, as well as any 'forceThreadSafe' setting.Evaluates a String containing VTL using the context passed to thesetVelocityContext(org.apache.velocity.context.Context)method.Evaluates a String containing VTL using the current context, and returns the result as a String.booleanReturnstrueif this render() and eval() methods will catch exceptions thrown during rendering.intGet the maximum number of loops allowed when recursing.protected StringinternalEval(org.apache.velocity.context.Context ctx, String vtl) protected StringinternalRecurse(org.apache.velocity.context.Context ctx, String vtl, int count) Recursively evaluates a String containing VTL using the current context, and returns the result as a String.Recursively evaluates a String containing VTL using the current context, and returns the result as a String.voidsetCatchExceptions(boolean catchExceptions) Sets whether or not the render() and eval() methods should catch exceptions during their execution or not.voidsetParseDepth(int depth) Set the maximum number of loops allowed when recursing.voidsetVelocityContext(org.apache.velocity.context.Context context) voidsetVelocityEngine(org.apache.velocity.app.VelocityEngine ve) Allow user to specify a VelocityEngine to be used in place of the Velocity singleton.从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
构造器详细资料
-
RenderTool
public RenderTool()
-
-
方法详细资料
-
configure
Looks for deprecated parse depth and catch.exceptions properties, as well as any 'forceThreadSafe' setting.- 覆盖:
configure在类中SafeConfig- 参数:
parser- configuration values
-
setVelocityEngine
public void setVelocityEngine(org.apache.velocity.app.VelocityEngine ve) Allow user to specify a VelocityEngine to be used in place of the Velocity singleton.- 参数:
ve- VelocityEngine instance
-
setParseDepth
public void setParseDepth(int depth) Set the maximum number of loops allowed when recursing.- 参数:
depth- parse depth- 从以下版本开始:
- VelocityTools 1.2
-
setVelocityContext
public void setVelocityContext(org.apache.velocity.context.Context context) - 参数:
context- Velocity context
-
getParseDepth
public int getParseDepth()Get the maximum number of loops allowed when recursing.- 返回:
- parse depth
- 从以下版本开始:
- VelocityTools 1.2
-
setCatchExceptions
public void setCatchExceptions(boolean catchExceptions) Sets whether or not the render() and eval() methods should catch exceptions during their execution or not.- 参数:
catchExceptions- whether to catch exceptions- 从以下版本开始:
- VelocityTools 1.3
-
getCatchExceptions
public boolean getCatchExceptions()Returnstrueif this render() and eval() methods will catch exceptions thrown during rendering.- 返回:
- whether to catch exceptions
- 从以下版本开始:
- VelocityTools 1.3
-
eval
Evaluates a String containing VTL using the context passed to the
setVelocityContext(org.apache.velocity.context.Context)method. If this tool is request scoped, then this will be the current context and open to modification by the rendered VTL. If application or session scoped, the context will be a new wrapper around the configured context to protect it from modification. The results of the rendering are returned as a String. By default,nullwill be returned when this throws an exception. This evaluation is not recursive.- 参数:
vtl- the code to be evaluated- 返回:
- the evaluated code as a String
- 抛出:
Exception- if womething went wrong
-
recurse
Recursively evaluates a String containing VTL using the current context, and returns the result as a String. It will continue to re-evaluate the output of the last evaluation until an evaluation returns the same code that was fed into it.
- 参数:
vtl- the code to be evaluated- 返回:
- the evaluated code as a String
- 抛出:
Exception- if womething went wrong- 另请参阅:
-
eval
Evaluates a String containing VTL using the current context, and returns the result as a String. By default if this fails, then
nullwill be returned, though this tool can be configured to let Exceptions pass through. This evaluation is not recursive.- 参数:
ctx- the current Contextvtl- the code to be evaluated- 返回:
- the evaluated code as a String
- 抛出:
Exception- if womething went wrong
-
internalEval
- 抛出:
Exception
-
recurse
Recursively evaluates a String containing VTL using the current context, and returns the result as a String. It will continue to re-evaluate the output of the last evaluation until an evaluation returns the same code that was fed into it or the number of recursive loops exceeds the set parse depth.
- 参数:
ctx- the current Contextvtl- the code to be evaluated- 返回:
- the evaluated code as a String
- 抛出:
Exception- if womething went wrong
-
internalRecurse
protected String internalRecurse(org.apache.velocity.context.Context ctx, String vtl, int count) throws Exception - 抛出:
Exception
-