[prev in list] [next in list] [prev in thread] [next in thread] 

List:       velocity-user
Subject:    RE: Using Velocity within a web application but outside of web page generation
From:       "Chachad, Ketan" <Ketan.Chachad () atosorigin ! com>
Date:       2009-01-02 14:28:10
Message-ID: D012B83E0F96574A9DDE30389295022404140C16 () inahm2k3001 ! in ! atosorigin ! com
[Download RAW message or body]

Hi Steve,

Thank you for the explanation and the intilization code. The same works
for me.

Regards,
Ketan K. Chachad


-----Original Message-----
From: Steve Cohen [mailto:scohen@javactivity.org] 
Sent: Friday, January 02, 2009 6:18 PM
To: Velocity Users List
Subject: Re: Using Velocity within a web application but outside of web
page generation

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: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
>   
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: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic