速度找不到资源 [英] Velocity can't find resource

查看:36
本文介绍了速度找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Something is wrong and it is very frustrating. I read on velocity's homepage that when I run a webapp then some properties should be set. And I've done that but no matter what I do I keep getting the same error.
This is where I set the props and use velocity

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/中.
如果我使用它,我会收到此错误:

The files is of course saved in /WEB-INF/mailtemplates/.
If I use this I get this error:

SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader.
SEVERE: The log message is null.

感谢您的时间:)

推荐答案

您正在使用 Webapp 资源加载器,它用于由 Velocity Tools servlet 提供服务的页面.(它需要一些特殊的初始化才能找到 servlet 上下文的根).

You are using the Webapp resourceloader, which is intended for pages served by the Velocity Tools servlet. (It requires some special initialization to find the root of the servlet context).

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

I recommend you use the ClasspathResourceLoader, then put the files into WEB-INF/classes, or elsewhere in your classpath. This is really the most straight forward approach.

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

更多信息在这里:

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

这篇关于速度找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆