1

我需要生成一些自动邮件,所以我想为这个任务使用速度。我已经将所有速度 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)

我尝试了使用类加载器/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 但由于某种原因我无法让它工作。

4

1 回答 1

0

(在评论和编辑中回答。请参阅没有答案的问题,但问题在评论中解决(或在聊天中扩展)

OP写道:

更新已解决。

最后在浪费了 2 天时间并拔出很多头发并多次敲打头部之后,它解决了。这是我为解决这个问题所做的,添加了几个日志语句以确保它指向正确的模板文件夹,然后粘贴模板文件的绝对路径。注意反斜杠和正斜杠。需要记录文件夹路径并查看它的确切位置,然后继续调试。修复这么简单的东西真是让人头疼。

/*
 * 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

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

于 2015-01-26T15:35:44.343 回答