Apache Velocity - 未找到资源异常

时间:2012-12-03 04:27:12

标签: apache servlets tomcat7 velocity

我需要生成一些自动邮件,因此我想使用velocity来执行此任务。我已将所有速度jar复制到lib文件夹并创建了一个hello.vm模板并放在WEB-INF / templates文件夹中。以下是我的例外,

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'hello.vm'
userCount incremented to :1
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
    at indian.test.handleRequest(test.java:34)
    at org.apache.velocity.tools.view.VelocityViewServlet.doRequest(VelocityViewServlet.java:217)
    at org.apache.velocity.tools.view.VelocityViewServlet.doGet(VelocityViewServlet.java:182)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at listener.trimresponse.doFilter(trimresponse.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

我尝试了使用classloader / webapps加载资源的所有其他方法,但仍然是错误仍然相同。我使用netbeans 7.2.x和tomcat 7.27。感谢是否有人可以为此提出建议。

下面是我的速度属性文件

resource.loader = file
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = C:\Users\kiran\Desktop\Netbeans Projects\ourstory\web\WEB-INF\templates
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 2
runtime.log=/WEB-INF/logs/velocity.log 
runtime.log.logsystem.class=org.apache.velocity.runtime.log.Log4JLogSystem
runtime.log.logsystem.log4j.pattern=%d - %m%n
runtime.log.logsystem.log4j.file.size=10000
runtime.log.logsystem.log4j.file.backups=1

以下是我正在使用的servlet

import java.util.Properties;
import javax.servlet.http.*;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

public class test extends VelocityViewServlet {
    private String htmlTemplate = "hello.vm";

    VelocityContext context = new VelocityContext();

    @Override
    public Template handleRequest(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
   //    Properties props = new Properties();
    //    props.setProperty("resource.loader", "class");
    //    props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    //    props.setProperty("webapp.resource.loader.path", "/WEB-INF/templates/");

        VelocityEngine engine = new VelocityEngine();


        engine.init();

        Template template = null;

        try {
            context.put("name", "Velocity Test");           
                    template = engine.getTemplate(htmlTemplate);
           } catch (Exception e) {
             e.printStackTrace();
            System.err.println("Exception caught: " + e.getMessage());
        }
        return template;
    }
}

它的简单servlet但由于某种原因我无法使它工作。

1 个答案:

答案 0 :(得分:0)

(在评论和编辑中回答。见Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

更新已解决。

     

最后浪费了好两天并拉出了很多头发,多次敲打头部,它解决了。我做了解决这个问题,添加了几个日志记录语句以确保它指向模板文件夹然后将绝对路径设置为模板文件。注意反斜杠和正斜杠。需要记录文件夹路径并查看其正确查找的位置,然后保持dubugging。它很难解决这么简单的问题。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author kiran
 */
import java.io.File;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.http.*;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

public class Hellotest extends VelocityViewServlet {

    @Override
    public Template handleRequest(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        Properties prop = new Properties();
  //      prop.put("resource.loader", "class");
  //      prop.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        String absolutePath = new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();
        prop.put("file.resource.loader.path", absolutePath + "\\templates\\");
        System.out.println("absolute path is : " + absolutePath);       
        System.out.println("keyset is : " + prop.keySet()); 
        Enumeration em = prop.keys();
        while (em.hasMoreElements()) {
            String str = (String) em.nextElement();
            System.out.println(str + ": " + prop.get(str));
        }
        VelocityEngine Velocity = new VelocityEngine();
        Velocity.init(prop);
        Template template = null;
        context.put("name", "Velocity Test");
        try {
            System.out.println("absolute path inside is : " + context); 
            template = Velocity.getTemplate("hello.vm","UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Exception caught: " + e.getMessage());
        }

        return template;
    }
}
  

这里是更新的速度属性文件,

resource.loader = file
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 2
  

最后我将模板放在webpages文件夹下。也许我会尝试在web-inf中推送它,看看它是怎么回事。鉴于我们正在公开模板,将模板保留在网页下并不是一件好事。