23

Today I have upgraded my entire Spring web application from using Spring 3.1.1 to Spring 3.2.

Most part of my existing app does not break except that in Spring 3.2, the

org.springframework.ui.velocity.VelocityEngineUtils

class seems to be removed completely from the spring-context-3.2.0.RELEASE.jar.

I found the migration guide as in this url.

It stated that org.springframework.ui.velocity.VelocityEngineUtils class has just been deprecated, but in fact it has been removed completely.

Maybe I'm just mistaken, I would like to know if the VelocityEngineUtils class still exist in somewhere or if not, what is the alternative class that I can use.

EDIT: It seems the entire velocity package has been removed from Spring 3.2 so now even org.springframework.ui.velocity.VelocityEngineFactoryBean does not exist. Are Spring walking away from Velocity?

j0k
  • 22,600
  • 28
  • 79
  • 90
woraphol.j
  • 1,301
  • 5
  • 14
  • 22

4 Answers4

41

I wanted to add a complete answer.

First, you add the dependency:

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.6.4</version>
</dependency>

Then, if you had a custom VelocityEngineFactory like this;

@Bean
public VelocityEngineFactory velocityEngine(){
    VelocityEngineFactoryBean bean = new VelocityEngineFactoryBean();
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty("resource.loader", "class");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    bean.setVelocityProperties(properties);
    return bean;
}

you need to replace it with a bean definition, something like below (in your @Configuration class); The below definition lets you to load templates from classpath.

@Bean
public VelocityEngine velocityEngine() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty("resource.loader", "class");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine velocityEngine = new VelocityEngine(properties);
    return velocityEngine;
}

Last, use it as: (here registration.vm is found on classpath)

@Autowired
private VelocityEngine velocityEngine;

public String prepareRegistrationEmailText(User user) {
    VelocityContext context = new VelocityContext();
    context.put("username", user.getUsername());
    context.put("email", user.getEmail());
    StringWriter stringWriter = new StringWriter();
    velocityEngine.mergeTemplate("registration.vm", "UTF-8", context, stringWriter);
    String text = stringWriter.toString();
    return text;
}

Good luck.

bekce
  • 3,782
  • 29
  • 30
19

Regarding the deprecation of VelocityEngineUtils in Spring, the Spring documentation isn't very clear on what to use instead.

It just says:

Deprecated. Use mergeTemplateIntoString(VelocityEngine, String, String, Map) instead, following Velocity 1.6's corresponding deprecation in its own API.

And to make it more confusing, the link refers back to itself.

Basically it's just saying use Velocity itself.

Here's a description of how you do that..

 // instead of a model map, you use a VelocityContext

 VelocityContext velocityContext = new VelocityContext();
 velocityContext.put("key1", value1);
 velocityContext.put("key2", value2);

 // the velocityEngine you wired into Spring has a mergeTemplate function
 // you can use to do the same thing as VelocityEngineUtils.mergeTemplate
 // with the exception that it uses a writer instead of returning a String      

 StringWriter stringWriter = new StringWriter();
 velocityEngine.mergeTemplate("templateName.vm", "UTF-8", velocityContext, stringWriter);

 // this is assuming you're sending HTML email using MimeMessageHelper

 message.setText(stringWriter.toString(), true);
Chris Jensen
  • 291
  • 3
  • 4
  • this comment has been extremely helpful but I don't see where you add the `Map model` into the template such as in `VelocityEngineUtils.mergeTemplateIntoString` which accepts a `model` argument. You're only merging the template content with the context and not the data model. – fIwJlxSzApHEZIl Apr 25 '17 at 17:51
  • Later note for anyone using `VelocityEngineUtils.mergeTemplateIntoString` you need to just call `.put()` on your pre-existing data model and add `"date, new DateTool()"` to your pre-existing model. My project's template syntax is slightly different too where variables are preceded with both a `$` character and a `!` character: `$date.format('yyyy-MM-dd', $!application.dateOfBirth)` – fIwJlxSzApHEZIl Apr 25 '17 at 18:05
  • This is the correct answer since quite sometime. – Thomas Carlisle Jun 02 '21 at 15:35
10

I don't think VelocityEngineUtils was in the spring-context jar (at least not since Spring last 3.1.x releases, according to GitHub).

Anyway, you can find it in spring-context-support-3.2.0.RELEASE.jar

madth3
  • 7,275
  • 12
  • 50
  • 74
  • The velocity classes were once in the spring-context.jar and my code relied on that. Anyway thank for your quick reply :) – woraphol.j Jan 14 '13 at 08:05
  • @woraphol.j You must have had some very odd mix of spring jars in that case. The Velocity classes has been in the spring-context-support module/jar since at least version 2.5. – pap Jan 14 '13 at 10:13
  • Latest POM reference (substitute your Spring version of course) ` org.springframework spring-context-support ${spring.framework.version} ` – hoserdude Jan 31 '14 at 01:06
1

VelocityEngineUtils is deprecated in spring > 3.2

Below dependency will work perfectly

<dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-webmvc-velocity</artifactId>
            <version>1.4.3.18.RELEASE</version>
 </dependency>

It's recommended to deprecate Velocity altogether and replace with thymeleaf

hitesh bedre
  • 459
  • 2
  • 11