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