Announcement Announcement Module
Collapse
No announcement yet.
How to configure Velocity tools? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    <tool>
        <key>foo</key>
        <class>our.namespace.velocity.tools.Foo</class>
        <scope>request</scope>
    </tool>
    Code:
    <data type="string">
        <key>stylesheetPath</key>
        <value>/foo/style</value>
    </data>
    In the old app, this file is loaded by the VelocityViewServlet from a file specified in the view servlets part of web.xml:

    Code:
    <servlet>
        ...
        <init-param>
            <param-name>org.apache.velocity.toolbox</param-name>
            <param-value>/WEB-INF/toolbox.xml</param-value>
        </init-param>
    </servlet>
    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

    VelocityToolboxView

    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.

    Comment


    • #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.

      Comment


      • #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?

        Comment


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

          Code:
          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;
          		setViewClass&#40;VelocityToolboxView.class&#41;;
          	&#125;
          
              /**
               * 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 org.apache.velocity.tools.view.servlet.ServletToolboxManager#getInstance
               */
              public void setToolboxConfigLocation&#40;String toolboxConfigLocation&#41; &#123;
                  this.toolboxConfigLocation = toolboxConfigLocation;
              &#125;
          
              protected View loadView&#40;String viewName, Locale locale&#41; throws BeansException &#123;
                  VelocityToolboxView view = &#40;VelocityToolboxView&#41;super.loadView&#40;viewName, locale&#41;;
                  view.setToolboxConfigLocation&#40;toolboxConfigLocation&#41;;
                  return view;
          	&#125;
          &#125;
          It can be used instead of the standard view resolver like this:
          Code:
              <!-- View resolver responsible for configuring VelocityToolboxViews -->
              <bean id="viewResolver" class="my.namespace.VelocityToolboxViewResolver">
                  ...
                  <property name="toolboxConfigLocation">
                      <value>/WEB-INF/velocitytoolbox.xml</value>
                  </property>
              </bean>

          Comment


          • #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.

            Comment


            • #7
              Thanks!

              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,

              Paul.

              Comment


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

                Comment

                Working...
                X