VelocityEngineUtils在Spring 3.2中已经被移除,那么还有什么其他替代品可用?

23

今天我将整个Spring Web应用程序从使用Spring 3.1.1升级到Spring 3.2

我的现有应用程序大部分都没有出现问题,除了在Spring 3.2中,

org.springframework.ui.velocity.VelocityEngineUtils

spring-context-3.2.0.RELEASE.jar 中,class 似乎已被彻底删除。

我在网址找到了迁移指南。

它说明 org.springframework.ui.velocity.VelocityEngineUtils 类仅仅已经被弃用,但实际上它已经被完全删除了。

也许我只是搞错了,我想知道是否还有 VelocityEngineUtils 类存在或者如果没有的话,我可以使用什么替代类。

编辑:看起来整个 velocity 包已经从 Spring 3.2 中删除,所以现在连 org.springframework.ui.velocity.VelocityEngineFactoryBean 都不存在了。Spring是否正在放弃Velocity?

4个回答

41

我想提供一份完整的答案。

首先,您需要添加依赖项:

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

然后,如果你有一个像这样的自定义VelocityEngineFactory

@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;
}

你需要将其替换为一个bean定义,类似于以下内容(在你的@Configuration类中);下面的定义允许您从类路径加载模板。

你需要用bean定义替换它,在你的@Configuration类中加入以下定义,这个定义可以让你从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;
}

最后,按如下方式使用:(假设 registration.vm 在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;
}

祝你好运。


你好,谢谢。"is found on classpath" 是什么意思? - OJVM

19

关于Spring中VelocityEngineUtils的弃用,Spring文档并不清楚应该使用什么替代方法。

文档上只是这样写:

已弃用。取而代之,请使用mergeTemplateIntoString(VelocityEngine, String, String, Map),按照Velocity 1.6自身API中对应的弃用进行更新。

为了让人更加困惑,链接指向了本身。

基本上它只是在说要使用Velocity本身。

以下是如何实现的说明。

 // 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);

这条评论非常有帮助,但我不知道在模板中添加 Map<String, Object> model 的位置,比如在 VelocityEngineUtils.mergeTemplateIntoString 中接受一个 model 参数。你只是将模板内容与上下文合并,而不是数据模型。 - fIwJlxSzApHEZIl
对于使用 VelocityEngineUtils.mergeTemplateIntoString 的任何人,稍后请注意,您只需要在现有数据模型上调用 .put() 并将 "date, new DateTool()" 添加到现有模型中即可。我的项目模板语法略有不同,其中变量前面有 $ 字符和 ! 字符:$date.format('yyyy-MM-dd', $!application.dateOfBirth) - fIwJlxSzApHEZIl
这是正确答案已经有一段时间了。 - Thomas Carlisle

10

我认为VelocityEngineUtils不在spring-context jar包中(至少自从 GitHub 上的 Spring 最后一个 3.1.x 版本以来是这样)。

无论如何,你可以在spring-context-support-3.2.0.RELEASE.jar中找到它。


速度类曾经在 spring-context.jar 中,我的代码依赖于它。无论如何,感谢您的快速回复 :) - woraphol.j
@woraphol.j 在这种情况下,你一定使用了一些非常奇怪的Spring Jars组合。Velocity类至少从2.5版本开始就在spring-context-support模块/ jar中了。 - pap
最新的POM参考(当然要替换为您的Spring版本) org.springframework spring-context-support ${spring.framework.version} - hoserdude

1

VelocityEngineUtils在Spring > 3.2中已被弃用。

以下依赖项将完美地工作。

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

建议完全弃用Velocity并改用thymeleaf


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接