我正在開發一個使用servlet的Web應用程序,其中我想使用Velocity
模板。爲此,我已將我的模板保存在WEB-INF/templates
目錄下。無法讀取Java Web應用程序中的文件
我有另一個普通的Java類,它從這個模板生成代碼,並需要讀取此文件。
我的servlet從這個簡單的java類調用方法來獲取生成的代碼。
我試圖用真正的路徑來閱讀模板,但我每次訪問servlet時都會收到ResourceNotFoundException
。
以下是我使用的sevlet其中
TemplateParser parser = new TemplateParser(getServletContext().getRealPath("/WEB-INF/templates/index.vm"));
String html = parser.parser();
out.println(html);
TemplateParser
是我簡單的Java類的代碼。
我在Netbeans
項目中使用它,生成的路徑字符串是 C:\Users\Sushil Kumar\Documents\NetBeansProjects\ALTTC\build\web\WEB-INF\templates\index.vm
。該文件退出給定的路徑,但仍然得到例外。
異常
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'C:\Users\Sushil Kumar\Documents\NetBeansProjects\ALTTC\build\web\WEB-INF\templates\index.vm'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:452)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1077)
at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:528)
at me.uni.sushilkumar.alttc.TemplateParser.parser(TemplateParser.java:52)
at me.uni.sushilkumar.alttc.TemplateController.processRequest(TemplateController.java:38)
at me.uni.sushilkumar.alttc.TemplateController.doGet(TemplateController.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
編輯的堆棧跟蹤作爲意見建議的TheNewIdiot。
Properties p = new Properties();
p.setProperty("resource.loader", "file");
p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
p.setProperty("file.resource.loader.path", path); <== path = getServletContext().getRealPath("/WEB-INF/templates")
p.setProperty("file.resource.loader.cache", "false");
p.setProperty("file.resource.loader.modificationCheckInterval", "0");
您可以粘貼堆棧跟蹤嗎? – NINCOMPOOP
@TheNeiIdiot我已添加堆棧跟蹤。 – kaysush
[無法找到速度模板資源]的可能重複(http://stackoverflow.com/questions/9051413/unable-to-find-velocity-template-resources) – NINCOMPOOP