Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VelocityEngineUtils has been removed in Spring 3.2 so what else to use?

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?

like image 297
woraphol.j Avatar asked Jan 14 '13 06:01

woraphol.j


People also ask

Does spring 5 support velocity?

Velocity support has been removed as of Spring Framework 5 and therefore our support as well (even deprecated in earlier version).

Is velocity deprecated?

Well, the reason is that the Velocity Engine has been deprecated for a while, and a lot of developers around the world need to find well-fitting alternatives.

What is Velocityengine in Java?

Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.

What is Apache Velocity used for?

Velocity can be used to generate web pages, SQL, PostScript and other output from templates. It can be used either as a standalone utility for generating source code and reports, or as an integrated component of other systems.


2 Answers

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.

like image 200
bekce Avatar answered Oct 22 '22 06:10

bekce


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);
like image 29
Chris Jensen Avatar answered Oct 22 '22 06:10

Chris Jensen