java - 速度找不到资源

标签 java web-applications velocity

出了点问题,这很令人沮丧。我在 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

更多信息在这里:

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

关于java - 速度找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3443819/

相关文章:

java - 获取 hibernate 的 userId 的 PropertyAccessException

javascript - 将 dotcms 2.5.7 升级到 3.7.2 $folder = $fileRepository_filesList.get(0).folder - 不会返回文件夹 ID

java - 如何解决错误 :Left side ($point) of '>=' operation has null value

java - 我如何 Autowiring 速度模板?

php - 将 php 应用程序商业化?

php - 设置来自第三方域的 cookie

java - 如何从终端接收输入并正确使用它

java - 如何在Java中使用方括号进行数学运算?

java - elasticsearch Elasticsearch集成测试

javascript - SproutCore:在Buildfile中设置代理不起作用