如何将速度模板加载到EJB中用作邮件模板 [英] How to load a velocity template into an EJB to be used as a mail template

查看:86
本文介绍了如何将速度模板加载到EJB中用作邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java EE 6应用程序,我想在其中使用Velocity从模板生成邮件.我有一个@Named bean,它负责加载和填充特定的模板.该项目是一个Web应用程序,因此我将模板放入WEB-INF/classs中(虽然看起来很丑陋,但到目前为止我还没有找到一个更优雅的解决方案),然后使用ClasspathResourceLoader来访问文件.配置如下:

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)

因此,我需要将ServletContext移交给速度引擎.但是我的bean不知道该上下文,因此我不想使用servlet来发送邮件.前端是使用JSF 2.0实现的,因此我可以访问FacesContext.getCurrentInstance().getExternalContext().getContext(),将其强制转换为ServletContext并将其提供给引擎.但是上下文始终为空,我只是不知道如何使所有内容正常工作.每个提示/解决方案都受到高度赞赏:)

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 :)

预先感谢, 亚历克斯

推荐答案

Velocity可以在任何环境中运行,因此不需要servlet.似乎该问题与日志记录工具有关,日志记录工具的默认值取决于servlet.

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.

您可以使用NullLogChute,或者根据您的日志记录框架,选择实现LogChute的类.因此,例如,如果您使用公共记录,则需要 CommonsLogLogChute .如何设置:

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:

要使用,请先设置commons-logging,然后通过将以下内容添加到Velocity.properties中来告诉Velocity使用此类进行日志记录:runtime.log.logsystem.class = org.apache.velocity.runtime.log. CommonsLogLogChute

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

您还可以设置此属性以指定Velocity的消息应记录到的日志/名称(默认示例如下). runtime.log.logsystem.commons.logging.name = org.apache.velocity

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

因此,除了在Velocity.properties中提供此设置之外,您还可以调用:

So, apart from providing this setting in velocity.properties, you can call:

engine.setProperty("runtime.log.logsystem.class", 
       "org.apache.velocity.runtime.log.CommonsLogLogChute");

这篇关于如何将速度模板加载到EJB中用作邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆