Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...