Chachad, Ketan wrote:
Hi,
In all the articles I have read for Web Applications, Velocity Templates
are used for generating web pages.
The requirement in my web application is to get the velocity template
and merge it with data retrieved.
This merged output is then transmitted for further processing.
Basically I want to use Velocity within a web application but outside of
web page generation.
Can anyone provide me with concrete examples or tutorials that would
help me with this?
Regards,
Ketan K. Chachad
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
This is not so different from what I just did earlier this week. In my
case, my app was a non-web app that happened for a variety of reasons to
be running inside of Tomcat. My app is an AOL Instant Messenger "bot"
whose UI is provided via AIM. The "further processing" I needed to do
was to send it to AIM for eventual retransmission to the end-user.
Velocity fit very nicely into this scenario. I simply initialized
Velocity to use the WebappLoader from VelocityTools. (I believe this has
been renamed VelocityResourceLoader in Tools 2.0 if you're going to use
that version). Velocity can then find the templates at the usual web
context path.
Anyway here is my initialization code. It is called from the init()
method of the main servlet that launches my application. This method has
access to the ServletContext that is passed in. BTW
VelocityInitializationException is my code - it wraps the "throws
Exception" coming out of the engine.init() call. engine is a
VelocityEngine object. The class this runs in is a singleton but you
don't have to do it that way. And the logging is set up to use an
existing log4j setup but you have other choices there as well.
private void init(ServletContext ctx) throws
VelocityInitializationException {
this.engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.Log4JLogChute" );
this.engine.setProperty("runtime.log.logsystem.log4j.logger",
"Velocity");
// the following three lines are the key to initializing Velocity to use
the WebappLoader.
this.engine.setProperty("resource.loader", "webapp");
this.engine.setProperty("webapp.resource.loader.class",
"org.apache.velocity.tools.view.servlet.WebappLoader");
this.engine.setApplicationAttribute("javax.servlet.ServletContext", ctx);
try {
this.engine.init();
} catch (Exception e) {
throw new VelocityInitializationException(e);
}
logHelper.info("following is initialization output from velocity");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]