You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Chris Searle <ch...@empolis.no> on 2004/03/17 14:12:26 UTC

Obtaining the velocity instance from within a class

Am running struts together with the velocity
tools/VelocityViewServlet.

I now need to create some complex mail messages (text) from within
a class called from a struts action.

Have looked at the example from 

  http://today.java.net/pub/a/today/2003/12/16/velocity.html

which gets a VelocityContext and initialises an instance of the
engine.

However - the VelocityViewServlet already has an instantiated engine
running.

Is there an easy way from within the action to get the running
velocity instance? Or do I have to init another instance?

Regards

-- 
Chris Searle
chris@empolis.no


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


Re: Obtaining the velocity instance from within a class

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On Mar 17, 2004, at 10:35 AM, Chris Searle wrote:

>>>>>> "SC" == Simon Christian <si...@stoutstick.com> writes:
>
>     SC> Personally I'd probably create a new instance of
>     SC> VelocityEngine. There would probably be queries over
>     SC> duplicated cached templates, but I'd guess that your mail
>     SC> templates don't crossover with your other templates.
>
> Correct - there's no overlap at all. The information isn't even stored
> in the same place (at the moment).
>
> I gave it a try - and it seems to have worked well (I was worried that
> there would be an overhead in initialising the engine - there may be -
> but not enough to worry about).
>

It should be minor.  What you could do to avoid that is just have an 
instance configured statically and reused.
-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


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


Re: Obtaining the velocity instance from within a class

Posted by Simon Christian <si...@stoutstick.com>.

Chris Searle wrote:
> 
> I gave it a try - and it seems to have worked well (I was worried that
> there would be an overhead in initialising the engine - there may be -
> but not enough to worry about).
> 
> Thanks for the advice.
> 

Chris,

Glad it's working ok for you. I probably should have suggested - in case 
you're not already doing this - to create the VelocityEngine instance 
just the once rather than on a per-request basis. This may be simplest 
to do by creating and storing it in the application context the first 
time it's needed (I'm sure there are lots of alternative ways to do it!).

- simon

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


Re: Obtaining the velocity instance from within a class

Posted by Chris Searle <ch...@empolis.no>.
>>>>> "SC" == Simon Christian <si...@stoutstick.com> writes:

    SC> Personally I'd probably create a new instance of
    SC> VelocityEngine. There would probably be queries over
    SC> duplicated cached templates, but I'd guess that your mail
    SC> templates don't crossover with your other templates.

Correct - there's no overlap at all. The information isn't even stored
in the same place (at the moment).

I gave it a try - and it seems to have worked well (I was worried that
there would be an overhead in initialising the engine - there may be -
but not enough to worry about).

Thanks for the advice.

-- 
Chris Searle
chris@empolis.no


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


Re: Obtaining the velocity instance from within a class

Posted by Simon Christian <si...@stoutstick.com>.
I believe that at the moment the VelocityViewServlet uses the Velocity 
singleton, so you can just reference it, or more specifically the 
RuntimeSingleton directly, something like:

 >
 > .
 > .
 > Context context = new VelocityContext();
 > context.put( "aString", myString );
 > context.put( "anArray", myArray ); // etc
 > Template template = RuntimeSingleton.getTemplate(name);
 > StringWriter writer = new StringWriter();
 > template.merge(context, vw);
 >

However, I don't think this is really the recommended usage, due to a 
few gotchas:

  - it's likely that VelocityViewServlet will move away from using the 
Velocity singleton and use VelocityEngine instead in later versions
  - you're reliant on the VelocityViewServlet being initialised before 
you attempt to use Velocity, typically that might not happen until the 
first output template is being rendered

Personally I'd probably create a new instance of VelocityEngine. There 
would probably be queries over duplicated cached templates, but I'd 
guess that your mail templates don't crossover with your other templates.

- simon


Chris Searle wrote:
> Am running struts together with the velocity
> tools/VelocityViewServlet.
> 
> I now need to create some complex mail messages (text) from within
> a class called from a struts action.
> 
> Have looked at the example from 
> 
>   http://today.java.net/pub/a/today/2003/12/16/velocity.html
> 
> which gets a VelocityContext and initialises an instance of the
> engine.
> 
> However - the VelocityViewServlet already has an instantiated engine
> running.
> 
> Is there an easy way from within the action to get the running
> velocity instance? Or do I have to init another instance?
> 
> Regards
> 

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