gpt4 book ai didi

java - 无法读取 Java Web 应用程序中的文件

转载 作者:行者123 更新时间:2023-12-01 17:23:29 26 4
gpt4 key购买 nike

我正在使用 servlet 开发一个 Web 应用程序,我想在其中使用 Velocity 模板。为此,我将模板保存在 WEB-INF/templates 目录下。

我有另一个普通的 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");

最佳答案

不要使用FileResourceLoader。看看documentation :

The simplest replacement for FileResourceLoader in a web application is actually a part of the VelocityTools project. It is the WebappResourceLoader. This ResourceLoader implementation is specifically designed to work just like the FileResourceLoader, but it is aware of the servlet context and allows you to configure resource paths relative to the servlet root, rather than the local file system.

If you are using the VelocityViewServlet, then it is automatically configured and ready to use the WebappResourceLoader. So if you want to change the configured path(s), you need only add a line like the following to your velocity.properties:

webapp.resource.loader.path=/WEB-INF/mytemplates/

If you need to set the WebappResourceLoader up on your own, then you can make your properties something like this:

resource.loader=webapp
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
webapp.resource.loader.path=/WEB-INF/mytemplates/

或者:

Properties props = new Properties();
props.setProperty("resource.loader", "webapp");
props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
props.setProperty("webapp.resource.loader.path", "/WEB-INF/mytemplates/");
VelocityEngine engine = new VelocityEngine(props);

You will also need to put the ServletContext into your VelocityEngine's application attributes before initializing that Engine. This is how the WebappResourceLoader knows how to find templates.

myVelocityEngine.setApplicationAttribute("javax.servlet.ServletContext", servletContext);

关于java - 无法读取 Java Web 应用程序中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16833577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com