Velocity无法找到资源

AnA*_*ser 17 java velocity web-applications

出了点问题,非常令人沮丧.我在velocity的主页上看到,当我运行webapp时,应该设置一些属性.而且我已经做到了,但无论我做什么,我都会遇到同样的错误.
这是我设置道具和使用速度的地方

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;
    }
...
}
Run Code Online (Sandbox Code Playgroud)

这些文件当然保存在/ WEB-INF/mailtemplates /中.
如果我使用这个我得到这个错误:

SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader.
SEVERE: The log message is null.
Run Code Online (Sandbox Code Playgroud)

感谢您的时间:)

Wil*_*ass 27

您正在使用Webapp资源加载器,该资源加载器适用于Velocity Tools servlet提供的页面.(它需要一些特殊的初始化来查找servlet上下文的根).

我建议您使用ClasspathResourceLoader,然后将文件放入WEB-INF/classes或类路径中的其他位置.这确实是最直接的方法.

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:

https://velocity.apache.org/engine/1.7/apidocs/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html