Mark Estrada
Mark Estrada

Reputation: 9191

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?

Upvotes: 0

Views: 475

Answers (1)

Claude Brisson
Claude Brisson

Reputation: 4130

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

Upvotes: 1

Related Questions