在 JSP 文件中导入具有 JSTL 核心的 XML 文件时请求的资源不可用

Requested resource is not available when importing XML file with JSTL core in JSP file

我是 Java EE 的新手,我刚刚开始学习 JSTL1 中的 XML 库。 2核.

我正在尝试编写一个简单的 JSP 文件来读取 XML 文件并显示它。 问题是当我尝试导入一个 XML 文件时,服务器抛出一个关于导入 XML 文件的行的异常说:

The requested resource (/test/document.xml) is not available.

这是我的 JSP 文件:

<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<c:import url="document.xml" varReader="documentReader" >
    <c:out value="${ documentReader }" />
</c:import>
</body>
</html>

XML 文件 document.xml 位于我项目的根目录中。 这是服务器 Apache Tomcat 7.0.54 抛出的异常:

SEVERE: "Servlet.service()" pour la servlet default a lancé une exception
java.io.FileNotFoundException: The requested resource (/test/document.xml) is not available
    at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:807)
    at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:442)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:314)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireReader(ImportSupport.java:341)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.doStartTag(ImportSupport.java:151)
    at org.apache.jsp.test_005fel_jsp._jspx_meth_c_005fimport_005f0(test_005fel_jsp.java:109)
    at org.apache.jsp.test_005fel_jsp._jspService(test_005fel_jsp.java:77)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

avr. 01, 2015 8:38:13 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/test] threw exception [An exception occurred processing JSP page /test_el.jsp at line 9

6: <title>Création d'un client.</title>
7: </head>
8: <body>
9: <c:import url="document.xml" varReader="monReader" >
10:     <c:out value="${ monReader }" />
11: </c:import>
12: </body>


Stacktrace:] with root cause
java.io.FileNotFoundException: The requested resource (/test/document.xml) is not available
    at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:807)
    at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:442)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:314)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireReader(ImportSupport.java:341)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.doStartTag(ImportSupport.java:151)
    at org.apache.jsp.test_005fel_jsp._jspx_meth_c_005fimport_005f0(test_005fel_jsp.java:109)
    at org.apache.jsp.test_005fel_jsp._jspService(test_005fel_jsp.java:77)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我尝试了其他方法来使用文件的绝对路径读取 XML 文件。这不是最终解决方案,而是部分解决方案,可以提供更多帮助。 我已将以下 value 赋予属性 url :

<c:import url="file:/C:/PathToWS/WorkSpace/JavaEEProject/document.xml" varReader="xmlReader" >
<c:out value="${ xmlReader }" />
</c:import>

我在 XML 文件的完整路径之前写了 file 来指定访问资源(本例中的文件)所使用的协议,知道它也可以是httpftp.

然后,我可以读取和解析存储在 xmlReader 中的 XML 文件的内容。

当然,XML文件不需要在javaEE项目的根目录下,它可以在磁盘的任何地方。