VelocityEngineUtils已经在Spring 3.2中删除了,还有什么可以使用的?

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

除了在Spring 3.2 ,我的现有应用程序的大多数部分都没有破坏

 org.springframework.ui.velocity.VelocityEngineUtils 

class似乎完全从spring-context-3.2.0.RELEASE.jar中删除

我在此url中找到了迁移指南。

它声明org.springframework.ui.velocity.VelocityEngineUtils类刚刚被弃用,但事实上它已被完全删除。

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

编辑:似乎整个速度包已从Spring 3.2删除,所以现在甚至org.springframework.ui.velocity.VelocityEngineFactoryBean都不存在。 spring是否会离开Velocity?

我不认为VelocityEngineUtils是在spring-context jar中(至少自从Spring上次发布3.1.x以来没有,根据GitHub)。

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

我想补充一个完整的答案。

首先,添加依赖项:

   org.apache.velocity velocity 1.6.4  

然后,如果您有这样的自定义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 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; } 

最后,将其用作:

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

祝你好运。

关于在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);