Spring VelocityContext无法获取值

Spring VelocityContext无法获取值,spring,velocity,Spring,Velocity,我使用Spring和模板engine Velocity发送邮件。 我试图将值设置为velocityContext,然后将其放入velocity模板中 VelocityContext velocityContext = new VelocityContext(); velocityContext.put("firstName", "Thomas"); 当我调试代码时,我可以看到velocityContext得到值“Thomas”,但由于某些原因,当我在视图中得到这

我使用
Spring
和模板
engine Velocity
发送邮件。 我试图将值设置为
velocityContext
,然后将其放入
velocity
模板中

VelocityContext velocityContext = new VelocityContext();
                velocityContext.put("firstName", "Thomas");
当我调试代码时,我可以看到
velocityContext
得到值“Thomas”,但由于某些原因,当我在视图中得到这个值时,我得到的只是
${firstName}
类似的字符串

这是我的密码

private void sendEmail(final String toEmailAddresses, final String fromEmailAddress,
                           final String subject, final String attachmentPath,
                           final String attachmentName) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                message.setTo(toEmailAddresses);
                message.setFrom(new InternetAddress(fromEmailAddress));
                message.setSubject(subject);
                VelocityContext velocityContext = new VelocityContext();
                velocityContext.put("firstName", "Thomas");
                String body = VelocityEngineUtils.mergeTemplateIntoString(
                        velocityEngine, "templates/registration.vm", "UTF-8", null);
                message.setText(body, true);
                if (!StringUtils.isBlank(attachmentPath)) {
                    FileSystemResource file = new FileSystemResource(attachmentPath);
                    message.addAttachment(attachmentName, file);
                }
            }
        };
        this.mailSender.send(preparator);
    }
和模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     <body>
         Hi, ${firstName}<br/> 
     </body>
</html>

你好,${firstName}

您没有使用您创建和初始化的
VelocityContext
。如果您使用的是
eclipseide
,这将显示为警告
VelocityEngineUtils
不接受
VelocityContext
作为其任何方法中的参数

有两种方法:

  • 或者,使用
    VelocityEngine
    ,然后调用
    VelocityEngine.mergeTemplate
    ,它将
    VelocityContext
    作为输入参数
  • 使用
    VelocityEngineUtils
    并传递将模板中的占位符映射为实际值的
    Map
    模型。我建议您这样做,因为您的用例看起来非常直接
而不是

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("firstName", "Thomas");
String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/registration.vm", "UTF-8", null);
使用

Map model=newhashmap();
模型。put(“名字”、“托马斯”);
String body=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,“templates/registration.vm”,“UTF-8”,model);
Map<String, Object> model = new HashMap<String, Object>();
model.put("firstName", "Thomas");
String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/registration.vm", "UTF-8", model);