未找到力度模板资源异常

时间:2012-11-06 10:11:57

标签: java servlets velocity

  

发生异常:   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);

2 个答案:

答案 0 :(得分:3)

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

Template template = Velocity.getTemplate("templates/ResourceLoader1.vm");

答案 1 :(得分:3)

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

与您的配置一起使用的FileResourceLoader不了解网络服务器和上下文及内容,因此您配置它的方式,它会在中查找WEB-INF文件夹文件系统的根您的应用程序服务器正在运行。