How to load a velocity template into an EJB to be used as a mail template
I have a Java EE 6 application in which I'd like to use velocity to generate mails from a template. I have a @Named bean which is responsible for loading and filling a particular template. The project is a web application, so I placed my templates into WEB-INF/classes (which btw seems to be rather ugly, but I didn't find a more elegant solution by now) and used the ClasspathResourceLoader to access the files. The configuration is as follows:
Properties props = new Properties();
props.setProperty("resource.loader", "class");
props.setProperty("resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngine engine = new VelocityEngine(props);
VelocityContext context = new VelocityContext();
engine.init();
context.put("myObject", myObject);
Template template = engine.getTemplate("mail_template.vm");
StringWriter write开发者_StackOverflow社区r = new StringWriter();
template.merge(context, writer);
Running this code produces the follwing exception:
Caused by: java.lang.UnsupportedOperationException: Could not retrieve ServletContext from application attributes
at org.apache.velocity.runtime.log.ServletLogChute.init(ServletLogChute.java:73)
at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)
So I'm required to hand in the ServletContext to the velocity engine. But my bean is not aware of that context and I don't want to use a servlet for sending mails. The frontent is implemented with JSF 2.0 so I can access FacesContext.getCurrentInstance().getExternalContext().getContext(), cast it to ServletContext and provide it to the engine. However the context is always null, and I just have no clue how to get everything up working. Every hint / solution is highly appreciated :)
Thanks in advance, Alex
Velocity can run in any environment, so servlets are not a requirement. It seems that the problem is related to the logging facility, whose default depends on a servlet.
You can use a NullLogChute
, or, depending on your logging framework, choose a class implementing LogChute
. So for example, if you use commons-logging, you'd need CommonsLogLogChute
. How to set it up:
To use, first set up commons-logging, then tell Velocity to use this class for logging by adding the following to your velocity.properties: runtime.log.logsystem.class = org.apache.velocity.runtime.log.CommonsLogLogChute
You may also set this property to specify what log/name Velocity's messages should be logged to (example below is default). runtime.log.logsystem.commons.logging.name = org.apache.velocity
So, apart from providing this setting in velocity.properties, you can call:
engine.setProperty("runtime.log.logsystem.class",
"org.apache.velocity.runtime.log.CommonsLogLogChute");
精彩评论