English 中文(简体)
VTL评估或定义对象引用
原标题:VTL evaluate or define an object reference

我想创建一个以字符串为参数并将其计算为对象的宏。类似于:

#macro( valueTest $objRef)
  #define( $obj )#evaluate("$${objRef}")#end
  $obj.foo ## This would have to be translated to $obj.getFoo()
#end

不幸的是,$obj变量没有指向可用于调用方法的对象引用$obj是一个字符串,$obj.foo不尝试执行getFoo。

为什么我有一种感觉,这就是评估的本质,不可能做我想做的事。

我之所以想这样做smth,是因为我们有相当多的宏同时将命令绑定路径和命令本身作为参数,我希望后者可以从第一个派生出来。

最佳回答
Unfortunately, Velocity does not have a mechanism to define functions which return object references.  Macros are really intended to be a shortcut to display text.

在这种情况下,通常的方法是用Java创建一个“工具”,并在上下文中放置一个实例。工具只是一个普通的类,它有一个返回所需内容的方法

例如。

使用“eval”方法创建一个对象,然后将其作为“referenceEvaluator”放入上下文中。

#set($obj = $referenceEvaluator.eval($objRef))

如果您避免双重求值,只在名为$obj的上下文中插入一个对象来执行您想要的操作,您可能会发现您的代码更加清晰。(性能也更好)。

问题回答

暂无回答




相关问题
NHibernate L2 Cache configuration in Fluent NHibernate

Is ti possible to configure the L2 cache provider in code via FHN? Adding a line to the following config is what I m after: return Fluently.Configure() .Database(MsSqlConfiguration....

velocity framework on google app engine

i am trying to use velocity framework on google app engine. i wrote a small program with a main method and tried running it locally. i get the following exception : Exception in thread "main" org....

Apache Velocity $ vs $!{}

For apache velocity, is there a difference between accessing variables with $ vs. $!{}. If so, what is it?

Velocity (VM) template: how to disable for a block of jquery

I have Apache Velocity. I have some jQuery code. I think VM doesn t like when I do things like $img.css("float","left"). How can I completely disable VM parsing within a block of HTML/Javascript ? ...

Spring MVC: Resolving the view based on User-Agent

Spring Version: 2.5.6 I want to resolve the view to a specific velocity file based on the value of the User-Agent header. My current line of thinking is an implementation similar to the ...

Encoding problems with Apache Velocity include

I have a velocity/html file. I m trying to include/parse some simple HTML into a page. If I just copy the text directly on the the main file, it works fine: Miranda Kerr hasn’t been a Victoria’s ...

热门标签