wor*_*l.j 12 java spring velocity
今天我将整个Spring Web应用程序从Spring升级3.1.1到Spring 3.2.
我现有的应用程序的大部分内容都不会破坏,除了在Spring 3.2中
org.springframework.ui.velocity.VelocityEngineUtils
Run Code Online (Sandbox Code Playgroud)
class似乎完全从spring-context-3.2.0.RELEASE.jar中删除.
我在此网址中找到了迁移指南.
它表示org.springframework.ui.velocity.VelocityEngineUtils该类已被弃用,但实际上它已被完全删除.
也许我只是错了,我想知道VelocityEngineUtils类是否仍然存在于某个地方或者如果没有,我可以使用的替代类是什么.
编辑:似乎整个速度包已从Spring中删除,3.2所以现在甚至
org.springframework.ui.velocity.VelocityEngineFactoryBean不存在.春天是否会离开Velocity?
bek*_*kce 24
我想补充一个完整的答案.
首先,添加依赖项:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.6.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后,如果你有这样的习惯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;
}
Run Code Online (Sandbox Code Playgroud)
你需要用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;
}
Run Code Online (Sandbox Code Playgroud)
最后,将其用作:
@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;
}
Run Code Online (Sandbox Code Playgroud)
祝好运.
小智 14
关于在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);
Run Code Online (Sandbox Code Playgroud)
根据GitHub的说法,我不认为VelocityEngineUtils是在spring-contextjar中(至少自从Spring上次发布3.1.x以来).
无论如何,你可以找到它 spring-context-support-3.2.0.RELEASE.jar
| 归档时间: |
|
| 查看次数: |
19911 次 |
| 最近记录: |