Velocity模板引擎:如何检查是否通过传递的参数在宏内设置了变量

问题描述 投票:3回答:1

我目前有一种工作方法,可以检查是否设置了变量。我是这样的

CHECKING EACH VARIABLE:
#if($!{vh})
vh instance found!<br>
Classname: ${vh.class.name}<br>
#end
#if($!{su})
su instance found!<br>
Classname: ${su.class.name}<br>
#end

当前将打印此:

检查每个变量:找到了vh实例!类别名称:de.integration.commons.VelocityXPathHelper

su实例被发现!类别名称:de.integration.commons.StringUtils

提示:我之前将这些变量注入到上下文中。

由于我确实需要检查很多变量,并且注入的变量有时会有所不同,所以我创建了一个应该执行相同操作的宏,并结合了包含已知注入变量的字符串数组的循环,如下所示:] >

#* Macro - checkIfVariableExists
This macro checks if variable exists with the quit reference notation
check http://velocity.apache.org/engine/devel/user-guide.html#quiet-reference-notation for more information *#
#macro (checkIfVariableExists $variablename)
    #if($!{variablename})
        $variablename instanz found!<br>
        Classname: ${variablename.class.name}<br>
    #end
#end

RESULT FROM LOOP:
#set( $toCheck = ["vh","su","anothervariable","...","....", "....."] )
#foreach( $value in $toCheck )
    #checkIfVariableExists($value)
#end

但是,这将不起作用,并导致此输出:

来自循环的结果:vh instanz找到了!类名:java.lang.Stringsu instanz找到了!类别名称:java.lang.String

我如何进行这项工作?我确实知道值是一个字符串,但是我想像第一种方法一样使用它的值。有任何想法吗?我使用速度引擎1.7,并且我无法更改为更高版本。

我目前有一种工作方法,可以检查是否设置了变量。我像这样检查每个变量:#if($!{vh})vh实例被发现!
类名:$ {vh.class.name} <...>

java velocity
1个回答
0
投票

如果正在检查,上下文中有什么可用,也可以使用Velocity ContextTool(https://velocity.apache.org/tools/3.0/apidocs/org/apache/velocity/tools/generic/ContextTool.html)。

© www.soinside.com 2019 - 2024. All rights reserved.