1

发生异常:org.apache.velocity.exception.ResourceNotFoundException:找不到资源“ResourceLoader1.vm”

我在 /WEB-INF/templates 中有 ResourceLoader1.vm,我坚持下来了,请帮帮我。

            Properties props = new Properties();  
            props.put("file.resource.loader.path", "/WEB-INF/templates");
            Velocity.init(props);
            VelocityContext context = new VelocityContext();
            context.put("greetings", "Many Happy returns");
            context.put("name", "Joseph");
            Writer writer = new StringWriter();
            Template template = Velocity.getTemplate("ResourceLoader1.vm");
            template.merge(context, writer);
4

2 回答 2

4

您应该将 .vm 模板相对于 CLASSPATH 放置。更好的选择是将 /templates 目录放在 WEB-INF/classes 下,丢失 props,然后像这样获取它:

Template template = Velocity.getTemplate("templates/ResourceLoader1.vm");
于 2012-11-06T10:16:27.430 回答
3

您似乎在 Web 应用程序中使用 Velocity。为此,您最好使用专为这种用途设计的VelocityViewServlet 。

与您的配置一起使用的FileResourceLoader那个不知道网络服务器和上下文和东西,所以您配置它的方式,它会在您的应用程序服务器运行的文件系统的根目录中WEB-INF查找一个文件夹。

于 2012-11-06T10:31:55.980 回答