Invoking VVS From Java Code

View: New views
3 Messages — Rating Filter:   Alert me  

Invoking VVS From Java Code

by White, Tim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello -
 
 My application has a Velocity View Servlet running (extremely well).
 
 In some cases, I'd like to use it to generate rendered output to a
String inside another part of the application.
 
 Is there some way I can latch onto the Velocity that VVS is using to
render a template without doing a URL connection over to VVS?
 
 I'd like to avoid duplicating all the setup logic that I've built for
VVS to initialize a separate Velocity engine for this purpose.
 
 Thanks,
 
Tim


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful.  If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.

Re: Invoking VVS From Java Code

by Nathan Bubna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Any chance you're using VelocityTools 2?  If so, then this is easy.
It was one of the big objectives for version 2 to make this simple.
All you need is either a FilterConfig, ServletConfig, or even just the
ServletContext and you can retrieve the VelocityView instance that the
VVS is using.

In VelocityTools 1.x, it's trickier and depends a bit on what you are
trying to do.   Here's the basics:

1. create a subclass of the VVS
2. have the subclass override init(ServletConfig) with a method that:
   a. calls super.init(config)
   b. puts the toolboxManager (a protected member variable) and the
result of getVelocityEngine() (a protected method) in the servlet
context attributes
3. in the code you wish to do this rendering, get the toolboxManager
and velocityEngine out of the servlet context attributes
4. create a ChainedContext using the engine, request, response (can be
null), and servlet context
5. call yourChainedContext.setToolbox(toolboxManager.getToolbox(yourChainedContext))
6. get your template from velocityEngine.getTemplate(templateName)
7. merge the template with yourChainedContext into the Writer of your choice

i suppose you might simplify some of the above by just putting the
servlet instance itself into the servlet context and having it do the
context creation and template retrieval through its createContext(...)
and getTemplate(name) methods, but it seems a little odd to be passing
a servlet itself around


On Mon, May 5, 2008 at 12:43 PM, White, Tim <Tim.White@...> wrote:

> Hello -
>
>   My application has a Velocity View Servlet running (extremely well).
>
>   In some cases, I'd like to use it to generate rendered output to a
>  String inside another part of the application.
>
>   Is there some way I can latch onto the Velocity that VVS is using to
>  render a template without doing a URL connection over to VVS?
>
>   I'd like to avoid duplicating all the setup logic that I've built for
>  VVS to initialize a separate Velocity engine for this purpose.
>
>   Thanks,
>
>  Tim
>
>
>  This communication is the property of Qwest and may contain confidential or
>  privileged information. Unauthorized use of this communication is strictly
>  prohibited and may be unlawful.  If you have received this communication
>  in error, please immediately notify the sender by reply e-mail and destroy
>  all copies of the communication and any attachments.
>

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


RE: Invoking VVS From Java Code

by White, Tim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the detailed information, Nathan!  I already have a subclass
of the VVS, so I think I'll try that route, although I hope to upgrade
to VelocityTools2 for our Fall release...

-----Original Message-----
From: Nathan Bubna [mailto:nbubna@...]
Sent: Monday, May 05, 2008 2:43 PM
To: Velocity Users List
Subject: Re: Invoking VVS From Java Code

Any chance you're using VelocityTools 2?  If so, then this is easy.
It was one of the big objectives for version 2 to make this simple.
All you need is either a FilterConfig, ServletConfig, or even just the
ServletContext and you can retrieve the VelocityView instance that the
VVS is using.

In VelocityTools 1.x, it's trickier and depends a bit on what you are
trying to do.   Here's the basics:

1. create a subclass of the VVS
2. have the subclass override init(ServletConfig) with a method that:
   a. calls super.init(config)
   b. puts the toolboxManager (a protected member variable) and the
result of getVelocityEngine() (a protected method) in the servlet
context attributes 3. in the code you wish to do this rendering, get the
toolboxManager and velocityEngine out of the servlet context attributes
4. create a ChainedContext using the engine, request, response (can be
null), and servlet context 5. call
yourChainedContext.setToolbox(toolboxManager.getToolbox(yourChainedConte
xt))
6. get your template from velocityEngine.getTemplate(templateName)
7. merge the template with yourChainedContext into the Writer of your
choice

i suppose you might simplify some of the above by just putting the
servlet instance itself into the servlet context and having it do the
context creation and template retrieval through its createContext(...)
and getTemplate(name) methods, but it seems a little odd to be passing a
servlet itself around


On Mon, May 5, 2008 at 12:43 PM, White, Tim <Tim.White@...> wrote:
> Hello -
>
>   My application has a Velocity View Servlet running (extremely well).
>
>   In some cases, I'd like to use it to generate rendered output to a  
> String inside another part of the application.
>
>   Is there some way I can latch onto the Velocity that VVS is using to

> render a template without doing a URL connection over to VVS?
>
>   I'd like to avoid duplicating all the setup logic that I've built
> for  VVS to initialize a separate Velocity engine for this purpose.
>
>   Thanks,
>
>  Tim
>
>
>  This communication is the property of Qwest and may contain
> confidential or  privileged information. Unauthorized use of this
> communication is strictly  prohibited and may be unlawful.  If you
> have received this communication  in error, please immediately notify
> the sender by reply e-mail and destroy  all copies of the
communication and any attachments.
>

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


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