velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <mkien...@alaska.net>
Subject Re: toolbox.xml and email templates
Date Fri, 16 Jan 2004 22:13:25 GMT
Nathan Bubna <nathan@esha.com> wrote:
> so, to get a full chained context (like the VelocityViewServlet uses) in 
your
> Struts Action, do something like this:
> 
> specify the location of your toolbox.xml as a context param in your 
web.xml
> (instead of an init-param in your servlet's config):
> 
> <context-param>
>    <param-name>org.apache.velocity.toolbox</param-name>
>    <param-value>/WEB-INF/toolbox.xml</param-value>
> </context-param>
> 
> to a context to use, do something like this in your action code:
> 
> // this is how you get the ServletContext in an Action
> ServletContext servletCtx = getServlet().getServletContext();
> 
> // this will retrieve the context parameter
> String file = servletCtx.getInitParameter("org.apache.velocity.toolbox");
> 
> // this should get the *very same* ToolboxManager instance
> // that your VelocityViewServlet is using
> ToolboxManager toolMgr = ServletToolboxManager.getInstance(servletCtx, 
file);
> 
> // this creates a context with access to tools, req/res/sess/ctx, and 
their
> attributes
> ChainedContext context = new ChainedContext(null, request, response,
> servletCtx);
> context.setToolbox(toolMgr.getToolboxContext(ctx));


I want to keep my Email business logic separate from the web server logic.

So I've created a method in my Application (extends ActionServlet) object.  
(I already retrieve my MessageResources the same way).

	public ToolboxContext getToolboxContext()
	{
		ServletContext servletCtx = getServletContext();
		
		//	this will retrieve the context parameter
		 String file = servletCtx.getInitParameter("org.apache.velocity.toolbox");
		
		//	this should get the *very same* ToolboxManager instance
		//	that your VelocityViewServlet is using
		ToolboxManager aToolboxManager =  
ServletToolboxManager.getInstance(servletCtx, file);			 
		return aToolboxManager.getToolboxContext(ctx);
	}

I'm not certain what value should be set for "ctx" in 
getToolboxContext(ctx).
Is this servletCtx?

Once I have this method, I plan to use

	Context context = new VelocityContext();
	context.setToolbox(theApplicationObject.getToolboxContext());
	Velocity.mergeTemplate(aTemplateName, "ISO8859-1", aContext, writer);

which seems like it'd be sufficient for importing my toolbox tools.
Or am I overlooking something?


Also, what are the pros and cons of using the servlet's ToolboxContext 
compared with generating a ToolBoxContext directly?
 

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


Mime
View raw message