VelocityEngineUtils已在Spring 3.2中被删除,那么还有什么可以使用的呢?

2022-09-02 02:20:50

今天,我已经将整个Spring Web应用程序从使用Spring升级到Spring。3.1.13.2

我现有应用程序的大部分内容都不会中断,除了在春季,3.2

org.springframework.ui.velocity.VelocityEngineUtils

类似乎已从 spring-context-3.2.0.RELEASE.jar 中完全删除。

找到了这个网址中的迁移指南。

它指出类刚刚被弃用,但实际上它已被完全删除。org.springframework.ui.velocity.VelocityEngineUtils

也许我只是弄错了,我想知道VelocityEngineUtils类是否仍然存在于某个地方,或者如果没有,我可以使用的替代类是什么。

编辑:似乎整个速度包已经从春季移除,所以现在甚至不存在。春天会离开Velocity吗?3.2org.springframework.ui.velocity.VelocityEngineFactoryBean


答案 1

我想添加一个完整的答案。

首先,添加依赖项:

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

你需要用一个豆定义来代替它,如下图所示(在你的类中);下面的定义允许您从类路径加载模板。@Configuration

@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

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

祝你好运。


答案 2

关于在Spring中弃用VelocityEngineUtils,Spring文档并不清楚该使用什么。

它只是说:

荒废的。使用mergeTemplateIntoString(VelocityEngine,String,String,Map)代替,在Velocity 1.6在其自己的API中相应的弃用之后。

为了使它更加混乱,该链接又指向了自身。

基本上,它只是说使用速度本身。

以下是您如何执行此操作的说明。

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

推荐