How to upload a speed template to EJB that will be used as an email template

I have a Java EE 6 application in which I would like to use speed to create letters from a template. I have an @Named bean that is responsible for loading and filling out a specific template. The project is a web application, so I put my templates in WEB-INF / classes (which, by the way, seems pretty ugly, but I haven't found a more elegant solution yet) 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 throws the following 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 , . Frontent JSF 2.0, FacesContext.getCurrentInstance(). GetExternalContext(). GetContext(), ServletContext . , , . / :)

, Alex

+3
1

, . , , .

NullLogChute, , , , LogChute. , commons-logging, CommonsLogLogChute. :

, commons-logging, Velocity, , speed.properties: 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");
+2

Source: https://habr.com/ru/post/1774735/


All Articles