我需要生成一些自动邮件,所以我想为这个任务使用速度。我已经将所有速度 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 但由于某种原因我无法让它工作。