Java Velocity引擎找不到资源
在我的应用程序中,用户可以接收电子邮件模板。我正在使用Java Velocity引擎找不到资源,java,velocity,Java,Velocity,在我的应用程序中,用户可以接收电子邮件模板。我正在使用velocity engine发送电子邮件。电子邮件模板位于WEB-INF/Email\u tempaltes/…目录中 这是我的密码: String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, "UTF-8", model); 其中,templateLocation是String变量,它显然是这样的: "/emai
velocity engine
发送电子邮件。电子邮件模板位于WEB-INF/Email\u tempaltes/…
目录中
这是我的密码:
String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, "UTF-8", model);
其中,templateLocation
是String变量
,它显然是这样的:
"/email_templates/request-feedback.vm"
emailSender.sendEmail(mail.getEmail(), "info@somemail.com", "Your message was sent successfully", mail, "/classes/templates/request-sent-answer.vm");
以下是我发送邮件的方式:
private void sendEmail(final String toEmailAddresses, final String fromEmailAddress,
final String subject, final String attachmentPath,
final String attachmentName, final Mail mail, final String templateLocation) {
Properties properties = new Properties();
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setTo(toEmailAddresses);
message.setFrom(new InternetAddress(fromEmailAddress));
message.setSubject(subject);
Map<String, Object> model = new HashMap<String, Object>();
model.put("phone", mail.getPhone());
model.put("email", mail.getEmail());
model.put("message", mail.getMessage());
String body = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, templateLocation, "UTF-8", model);
message.setText(body, true);
if (!StringUtils.isBlank(attachmentPath)) {
FileSystemResource file = new FileSystemResource(attachmentPath);
message.addAttachment(attachmentName, file);
}
}
};
this.mailSender.send(preparator);
}
我怎样才能修好它?我应该在哪里存储电子邮件模板?提前谢谢
------------编辑:-------------
我已经创建了类目录AnderWEB-INF,并在那里添加了我的模板
现在我得到了如下模板:
"/email_templates/request-feedback.vm"
emailSender.sendEmail(mail.getEmail(), "info@somemail.com", "Your message was sent successfully", mail, "/classes/templates/request-sent-answer.vm");
并得到相同的错误:
Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource
我不知道mergeTemplateIntoString做什么,但另一个解决方案是从类路径检索模板并使用MessageFormat对其进行格式化
String result;
final StringWriter templateData = new StringWriter();
final InputStream templateIn = YourClassName.class.getClassLoader().getResourceAsStream(path);
for(int b=templateIn.read(); b != -1; b=templateIn.read()){
templateData.write(b);
}
templateIn.close();
result = MessageFormat.format(templateData.toString(), arguments);
你试过了吗我已经看到了,但我不明白应该添加什么资源。loader=class.resource.loader.class=org.apache.velocity.runtime.resource.loader.classpathResourceLoader在配置文件中,您可以尝试哪个配置文件?我没有任何配置文件,然后为示例velocity.properties创建一个,并将其放入web inf/classes中,顺便说一句,如果您将模板目录放入web-inf/classes中,它可能会自行工作(如一个链接答案中所建议的)