最近我需要初始化Velocity的DateTool,以便在生成的电子邮件中正确格式化日期.通常你用VelocityContext来做这件事,但是,因为我使用的是在xml中配置的spring的VelocityEngineFactoryBean,所以我得知 - 在使用VelocityEngineFactoryBean时如何初始化VelocityContext?
换句话说,我的设置:
webmvc-config.xml中
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
Run Code Online (Sandbox Code Playgroud)
MailSender.java
public class CustomMailSender {
@Autowired
private VelocityEngine velocityEngine;
private void sendMail(final Object backingObject) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
// Set subject, from, to, ....
// Set model, which then will be used in velocity's mail template
Map<String, Object> model = new HashMap<String, Object>();
model.put("backingObject", backingObject);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, …Run Code Online (Sandbox Code Playgroud)