Saturday 15 January 2011

java - Velocity engine unable to find resource -



java - Velocity engine unable to find resource -

in application user can receive email templates. i'm using velocity engine sending emails. email templates located in web-inf/email_tempaltes/... directory.

here code:

string body = velocityengineutils.mergetemplateintostring(velocityengine, templatelocation, "utf-8", model);

where templatelocation string variable obvious looks this:

"/email_templates/request-feedback.vm"

here how send mails:

private void sendemail(final string toemailaddresses, final string fromemailaddress, final string subject, final string attachmentpath, final string attachmentname, final mail service 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); }

i tested email sending on local machine , works fine, when deployed on server got exception:

org.apache.velocity.exception.resourcenotfoundexception: unable find resource /email_templates/request-feedback.vm'

how can prepare it? should store email templates? in advance!

-----------edited:----------- i've created classes directory ander web-inf , have added templates there.

and template this:

emailsender.sendemail(mail.getemail(), "info@somemail.com", "your message sent successfully", mail, "/classes/templates/request-sent-answer.vm");

and same error:

could not prepare mail; nested exception org.apache.velocity.exception.resourcenotfoundexception: unable find resource

i don't know mergetemplateintostring does, solution retrieve template classpath , format using 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);

java velocity

No comments:

Post a Comment