简体   繁体   English

为什么Apache Velocity无法处理多个点键

[英]Why Apache Velocity cannot handle multiple dot keys

I dont get this while using Apache Velocity 1.7. 我在使用Apache Velocity 1.7时没有得到这个。 When I have a vm like this 当我有一个这样的虚拟机

db.connection.url = $db.customer.environment.db_url

and a context like this... 和这样的背景...

VelocityContext context = new VelocityContext();
context.put("db.customer.environment.db_url", "//sample_db_conn");

I am getting this error 我收到此错误

Caused by: org.apache.velocity.exception.MethodInvocationException: Object 'java.lang.String' does not contain property 'environment' at db.properties.vm[line 2, column 42] 由以下原因引起:org.apache.velocity.exception.MethodInvocationException:对象'java.lang.String'在db.properties.vm中不包含属性'environment'[第2行,第42列]

But if I put it like this.. it works... 但是如果我这样说的话..就可以了...

context.put("db.db_url", "//sample_db_conn");

Not sure why having multiple "." 不知道为什么要有多个“。” in the context key is causing this error. 在上下文密钥中导致此错误。 Any hints how to get over this? 任何提示如何克服这一点?

The dot is used as a property accessor. 点用作属性访问器。 When Velocity sees $db.customer.environment.db_url , it will try to get an object from the context under the db key, then try to call getCustomer() or get("customer") on it, and so on. 当Velocity看到$db.customer.environment.db_url ,它将尝试从db键下的上下文中获取一个对象,然后尝试在其上调用getCustomer()get("customer") ,依此类推。

So using dots in keys is a pretty bad idea with Velocity - yet, there are some workarounds. 因此,对于Velocity,在键中使用点是一个非常糟糕的主意-但是,有一些解决方法。

You need to put the context in itself, with something like: 您需要将上下文本身放在其中,例如:

context.put("context", context);

and then in your template you will be able to do: 然后您就可以在模板中执行以下操作:

$context.get("db.customer.environment.db_url")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM