• Previous post: Tweet, tweet…
  • Comments
  • Leave a Reply

Configuring Velocity Tools 2.0 in Spring

posted on Tuesday, 28 September, 2010 at 12:35 am

Recently I stumbled upon a problem when configuring Velocity Tools 2.0 with Spring 3.0.4 using new xml configuration. This article should help you to solve this problem.

Problem lies in deprecated API in Velocity Tools 2.0 which is still used in Spring VelocityToolbox­View. To solve this, just create new view class:

package info.elepha.web.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.context.Context;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.ToolboxFactory;
import org.apache.velocity.tools.config.XmlFactoryConfiguration;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.web.context.support.ServletContextResource;

/**
 * Configures Velocity Toolbox View to use new XML Configuration in Spring
 *
 * @author Gregor "hrax" Magdolen
 */
public class VelocityToolboxView extends org.springframework.web.servlet.view.velocity.VelocityToolboxView {

        @Override
        protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
                // Create a ViewToolContext instance since ChainedContext is deprecated in Velocity Tools 2.0.
                ViewToolContext velocityContext = new ViewToolContext(
                                getVelocityEngine(), request, response, getServletContext());
                velocityContext.putAll(model);

                // Load a Configuration and publish toolboxes to the context when necessary
                if (getToolboxConfigLocation() != null) {
                        XmlFactoryConfiguration cfg = new XmlFactoryConfiguration();
                        cfg.read(new ServletContextResource(getServletContext(), getToolboxConfigLocation()).getURL());
                        ToolboxFactory factory = cfg.createFactory();

                        velocityContext.addToolbox(factory.createToolbox(Scope.APPLICATION));
                        velocityContext.addToolbox(factory.createToolbox(Scope.REQUEST));
                        velocityContext.addToolbox(factory.createToolbox(Scope.SESSION));
                }

                return velocityContext;
        }

}

And then configure Spring Application context as following:

<!-- Set up VelocityConfigurer bean -->
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <!-- configure as necessary, path to templates must be relative to web app root -->
        <property name="resourceLoaderPath" value="/WEB-INF/velocity"/>
</bean>

<!-- Set up VelocityViewResolver bean -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="viewNames" value="*.vm" />
        <!-- configure as necessary, path to toolbox.xml must be relative to web app root -->
        <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" />
        <property name="viewClass" value="info.elepha.web.view.VelocityToolboxView" />
</bean>

And that should do it :)

Comments

  1. velocityContex­t.putAll forces us to case velocityContext into map<string,object) and if thats done it throws an error at run time!:-(((

Leave a Reply

Personal information
Body

All fields marked with * are required.