简体   繁体   中英

How programmatically resolve apache velocity variable?

Velocity documentation tells that to resolve a variablev a #set derective or POJO with corresponding property should be used. Is there a way to provide a custome resolver? Eg when $client appears in template, resolver go to database, retrieve Client entity, get name field and fill $client place holder. Does velocity template engine support such extensions?

You may try with the ReferenceInsertionHandler, but its purpose is rather to filter (aka modify, alter in some way) displayed values than to fetch values from Velocity references.

Such a task would typically be done by the context objects themselves. That is, $client can contain an object which knows how to fetch and return the client's name on its toString() method.

You can also use objects with getters and setters, so that you can write $client.name as long as $client.address . You'll get yourself doing some kind of ORM . As a side note, there are existing libraries easing this task for Velocity.

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