On Fri, Apr 4, 2008 at 11:04 AM, Charles Harvey III <[EMAIL PROTECTED]> wrote:
>
>
>  Nathan Bubna said the following on 4/3/2008 2:29 PM:
>
>
> > On Thu, Apr 3, 2008 at 10:09 AM, Charles Harvey III <[EMAIL PROTECTED]>
> wrote:
> >
> >
> > > Well, I tried the drop-in replacement and it failed.  It can't find the
> > >  template.  Not sure why yet.  I'm digging through the Spring code to
> see
> > >  what it is doing differently than Velocity.
> > >
> > >
> >
> > that's a bummer.  and the only thing changed was replacing the tools
> > 1.x jar with the 2.0-beta.jar ?
> >
> >
>  Actually, I had moved from spring-1.2.9 to spring-2.5.2.  But I did this
> because
>  fixes to how spring loads up Velocity were made to the 2.x branch and not
> the 1.x
>  branch.  All I ever put into the spring config was the resourceLoaderPath
> so I
>  have never been sure how spring loads up templates.  Total mystery.  But,
> for some
>  reason, it started failing.  It's all action-at-a-distance to me.  So, I
> figured
>  out how to set the FileResourceLoader and use the ${webapp.root} key to do
> so.
>  Now all template loading works on spring-1.2.9 and spring-2.5.2 with
> tools-1.2,
>  tools-1.3, tools-1.4 and tools-2.0-beta1.  Whoopie!

cool!  so, it's using FileResourceLoader but getting the right path
for you?  i wonder if this would still work in an unexploded war
deployment?

> >
> > >
> > >  So, I also have to look into how the new VelocityTools loads tools and
> how
> > > it is
> > >  different from Spring.  I'm sure Spring is calling Velocity to do this,
> > > just not sure
> > >  where yet.  I can setup a VelocityEngine in the configs, but that isn't
> > > where the
> > >  tools.xml gets passed.
> > >  It has to be in this section of VelocityToolboxView:
> > >
> > >
> ------------------------------------------------------------------------------
> > >  // Load a Velocity Tools toolbox, if necessary.
> > >  if (getToolboxConfigLocation() != null) {
> > >   ToolboxManager toolboxManager = ServletToolboxManager.getInstance(
> > >           getServletContext(), getToolboxConfigLocation());
> > >   Map toolboxContext = toolboxManager.getToolbox(velocityContext);
> > >   velocityContext.setToolbox(toolboxContext);
> > >  }
> > >
> > > Can
> > > I simply
> > >  replace the ServletToolboxManager with XmlFactoryConfiguration?  Is
> > > ToolboxManager
> > >  still used?
> > >
> > >
> >
> >
> > no, there's no one-to-one correspondence between the old way and the
> > new way.  but if you are willing to hack on Spring (or copy this View
> > class into your own version and hack that), i can probably help.  what
> > is the method you got that code from trying to return?  is it called
> > per request?  what's the lifecycle of the class it belongs to?
> >
> >
>  That method returns a org.apache.velocity.context.Context.
>
>  --------------------------------------------------------------------------
>  protected Context createVelocityContext(
>        Map model, HttpServletRequest request, HttpServletResponse response)
> throws Exception {
>    // Create a ChainedContext instance.
>    ChainedContext velocityContext = new ChainedContext(
>            new VelocityContext(model), getVelocityEngine(), request,
> response, getServletContext());
>
>    // Load a Velocity Tools toolbox, if necessary.
>    if (getToolboxConfigLocation() != null) {
>        ToolboxManager toolboxManager = ServletToolboxManager.getInstance(
>                getServletContext(), getToolboxConfigLocation());
>        Map toolboxContext = toolboxManager.getToolbox(velocityContext);
>        velocityContext.setToolbox(toolboxContext);
>    }
>    return velocityContext;
>  }
>  --------------------------------------------------------------------------
>
>  The class itself is
>
>  org.springframework.web.servlet.view.velocity.VelocityToolboxView
>
> http://springframework.cvs.sourceforge.net/springframework/spring/src/org/springframework/web/servlet/view/velocity/VelocityToolboxView.java?view=markup
>
>  which extends
>  org.springframework.web.servlet.view.VelocityView:
>
> http://springframework.cvs.sourceforge.net/springframework/spring/src/org/springframework/web/servlet/view/velocity/VelocityView.java?view=markup
>
>  Either of these two classes is used only once, it is the
> VelocityViewResolver and
>  the VelocityLayoutViewResolver that are used on each request.

looks to me like VelocityToolboxView is called per-request, since the
context creation method takes a request and response as parameters.

>
> >
> > ideally,  1 thru 4 are all handled through a VelocityView object that
> > can be stored in the ServletContext and used to both retrieve
> > templates and create tool-supporting contexts.  if you can access to
> > the ServletConfig instance in your replacement/hacked Spring MVC
> > classes that do template loading and context creation, then this could
> > be as simple as a call to
> > o.a.v.tools.view.ServletUtils.getVelocityView(servletConfig).
> >
> >
> > as for creating a tool-supporting context, that is more work without
> > being able to use a VelocityView instance, but is still do-able.
> > rather than explain all the options there, i'll wait to see if you
> > need such explanation.  the basic idea is to create a
> > FactoryConfiguration object (probably XmlFactoryConfiguration will be
> > most familiar to you).  then use that to configure a ToolboxFactory
> > instance.  keep the ToolboxFactory around, don't re-init on each
> > request!  then have your class do the same work as
> > VelocityView.prepareToolboxes(request) on each request.  then create a
> > ViewToolContext instance and use that as the context for that request.
> >  looking through the VelocityView methods for configuring the
> > ToolboxFactory might also be enlightening.
> >
> >
>  Well, I am going to try replacing Spring's VelocityToolboxView with my own.
> From above
>  it looks as though I should be able to get at the ServletConfig if I need
> to.  I
>  will try figure out how to create a FactoryConfiguration object and then
> setup
>  a ToolboxFactory.  I'll post if I get it all figured out.


if you can get the ServletConfig, then you can just use a VelocityView
and let it take care of the FactoryConfiguration and ToolboxFactory
for you.  just have your context creation method do:

VelocityView view = ServletUtils.getVelocityView(servletConfig);
return view.getContext(request, response);

seriously.  with access to the ServletConfig, it's that easy.  then
you can tell the VelocityView what to do through your servlet's
init-params, and that's only necessary if you don't like the defaults
and conventions it uses automatically.  read the javadoc for it,
especially for the configure(ServletConfig, ToolboxFactory) method.

http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/view/VelocityView.html

the VelocityView was designed to make it as easy as possible to
integrate Velocity and VelocityTools into web frameworks.  the only
catch is that you need ServletConfig access.


>
>
>  Charlie
>
>
>
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [EMAIL PROTECTED]
>  For additional commands, e-mail: [EMAIL PROTECTED]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to