如何将速度模板加载到 EJB 中以用作邮件模板
我有一个 Java EE 6 应用程序,我想在其中使用速度从模板生成邮件。我有一个 @Named bean 负责加载和填充特定模板。该项目是一个Web应用程序,所以我将模板放入WEB-INF/classes中(顺便说一句,这似乎相当难看,但我现在还没有找到更优雅的解决方案)并使用ClasspathResourceLoader来访问文件。配置如下:
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 writer = new StringWriter();
template.merge(context, writer);
运行此代码会产生以下异常:
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)
因此我需要将 ServletContext 交给速度引擎。但我的 bean 不知道该上下文,并且我不想使用 servlet 来发送邮件。前端是用 JSF 2.0 实现的,因此我可以访问 FacesContext.getCurrentInstance().getExternalContext().getContext(),将其转换为 ServletContext 并将其提供给引擎。然而上下文总是空的,我只是不知道如何让一切正常工作。非常感谢每个提示/解决方案:)
提前致谢, 亚历克斯
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 writer = 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Velocity 可以在任何环境中运行,因此不需要 servlet。问题似乎与日志记录工具有关,其默认值取决于 servlet。
您可以使用
NullLogChute
,或者根据您的日志记录框架,选择一个实现LogChute
的类。例如,如果您使用 commons-logging,则需要CommonsLogLogChute
。如何设置:,除了在velocity.properties中提供此设置之外,您还可以调用:
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 implementingLogChute
. So for example, if you use commons-logging, you'd needCommonsLogLogChute
. How to set it up:So, apart from providing this setting in velocity.properties, you can call: