出了点问题,这很令人沮丧。我在 velocity 的主页上读到,当我运行一个 webapp 时,应该设置一些属性。我已经做到了,但无论我做什么,我都会不断遇到同样的错误。
这是我设置 Prop 和使用速度的地方
public class ConfirmationMailGenerator implements MailGenerator {
private BasicUser user;
private String htmlTemplate = "HTMLConfirmationMailTemplate.vsl";
private String plainTemplate = "PlainConfirmationMailTemplate.vsl";
public ConfirmationMailGenerator(BasicUser user) {
this.user = user;
}
public StringWriter generateHTML() throws Exception {
Properties props = new Properties();
props.setProperty("resource.loader", "wepapp");
props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
props.setProperty("webapp.resource.loader.path", "/WEB-INF/mailtemplates/");
VelocityEngine engine = new VelocityEngine(props);
VelocityContext context = new VelocityContext();
engine.init();
Map map = createDataModel();
context.put("user", map);
Template template = engine.getTemplate(htmlTemplate);
StringWriter writer = new StringWriter();
template.merge(context, writer);
return writer;
}
...
}
文件当然保存在/WEB-INF/mailtemplates/。
如果我使用它,我会收到此错误:
SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader.
SEVERE: The log message is null.
感谢您的宝贵时间:)
最佳答案
您正在使用 Webapp 资源加载器,它专用于由 Velocity Tools servlet 提供的页面。 (它需要一些特殊的初始化来找到 servlet 上下文的根)。
我建议您使用 ClasspathResourceLoader,然后将文件放入 WEB-INF/classes 或类路径中的其他位置。这确实是最直接的方法。
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
更多信息在这里:
关于java - 速度找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3443819/