如何将速度模板加载到 EJB 中以用作邮件模板

发布于 2024-10-01 19:24:08 字数 1288 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-10-08 19:24:09

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

您可以使用 NullLogChute,或者根据您的日志记录框架,选择一个实现 LogChute 的类。例如,如果您使用 commons-logging,则需要 CommonsLogLogChute。如何设置:

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

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

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

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文