Announcement Announcement Module
No announcement yet.
How to configure Velocity tools? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to configure Velocity tools?

    Hi. I'm in the process of porting a web app from Struts to Spring MVC. Our app uses Velocity to render views. In out old app, the Velocity templates contain references to names defined in a toolbox configuration xml file. Examples:

    <data type="string">
    In the old app, this file is loaded by the VelocityViewServlet from a file specified in the view servlets part of web.xml:

    We now use Velocity views through a Velocity engine instantiated by the VelocityConfigurer, and thus the VelocityViewServlet is not used.

    My question is: Using Spring, what is the proper way to configure Velocity with the data in the toolbox configuration xml file?

  • #2
    Check out the latest build 1.1.3. Added is


    Alternatively it is of course possible to add them by overiding
    protected void renderMergedTemplateModel(Map map, HttpServletRequest request, HttpServletResponse response)
    in VelocityView and add whatever tools you need to the map.


    • #3
      The VelocityToolboxView also overcomes problems you would have with the second approach, I had to modify the standard link tool before to work because it needed certain velocity tools context.


      • #4
        Thanks. How do i configure the view resolver to use VelocityToolboxView instead of the normal VelocityView, and how do i configure the toolboxConfigLocation property of each individual view object the engine instantiates?


        • #5
          I made my own view resolver to handle this:

          import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
          import org.springframework.web.servlet.view.velocity.VelocityToolboxView;
          import org.springframework.web.servlet.View;
          import org.springframework.beans.BeansException;
          import java.util.Locale;
           * Velocty view resolver with support for velocity toolbox configuration.
          public class VelocityToolboxViewResolver extends VelocityViewResolver &#123;
              private String toolboxConfigLocation;
          	 * Sets default viewClass to VelocityToolboxView.
          	 * @see #setViewClass
          	public VelocityToolboxViewResolver&#40;&#41; &#123;
               * Set a "toolboxConfigLocation", for example "/WEB-INF/toolbox.xml",
               * to automatically load a Velocity Tools toolbox definition file and expose
               * all defined tools in the specified scopes to the resolved view. If no config
               * location is specified, no toolbox will be loaded and exposed.
               * The specfied location string needs to refer to a ServletContext
               * resources, as expected by ServletToolboxManager which is part of
               * the view package of Velocity Tools.
               * @see
              public void setToolboxConfigLocation&#40;String toolboxConfigLocation&#41; &#123;
                  this.toolboxConfigLocation = toolboxConfigLocation;
              protected View loadView&#40;String viewName, Locale locale&#41; throws BeansException &#123;
                  VelocityToolboxView view = &#40;VelocityToolboxView&#41;super.loadView&#40;viewName, locale&#41;;
                  return view;
          It can be used instead of the standard view resolver like this:
              <!-- View resolver responsible for configuring VelocityToolboxViews -->
              <bean id="viewResolver" class="my.namespace.VelocityToolboxViewResolver">
                  <property name="toolboxConfigLocation">


          • #6
            None of the above is necessary, really.

            I was able to get the VelocityView toolbox stuff to work by adding

            <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>

            to my viewResolver (org.springframework.web.servlet.view.velocity.Vel ocityViewResolver) in action-servlet.xml, and adding my toolbox.xml file under WEB-INF. Make sure your toolbox.xml file doesn't contain Struts-specific tools in it (if you're using SpringMVC, that is).

            I mainly needed the VelocityView jar so I could have access to $response in my VMs. If Spring provides support for this elsewhere I'm not aware of it.
            I use $response to set the content type of my VM, like this:

            #set($temp = $response.setContentType("text/xml")).

            Setting the contentType in my controller's response object gets overridden when the view is rendered.

            Hope this helps someone else; took me a couple hours to figure out.


            • #7

              I was having the same problem after moving my Veloctiy project over to Spring. Adding the location of my toolbox (as stated above) has solved the problem.

              Many thanks,



              • #8
                No problem. Glad it helped someone else.