简体   繁体   中英

Why Apache Velocity cannot handle multiple dot keys

I dont get this while using 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]

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.

So using dots in keys is a pretty bad idea with Velocity - yet, there are some workarounds.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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