在 WAR 文件中访问 HTML

Access HTML within WAR file

我无法从我的 .war 文件访问我的 index.html。我将 index.html 放在根 war 文件路径中。这也是 META-INF 和 WEB-INF 文件夹所在的位置。当我尝试从 url http://localhost:8080/Test/index.html 访问它时,我得到

"JBWEB000065: HTTP Status 404 - Could not find resource for relative : /index.html of full path: http://localhost:8080/Test/index.html"

在我的 server.log 上,我看到服务器成功启动,没有任何错误。此外,当我尝试访问该页面时,我没有在 server.log 上获得任何堆栈跟踪。我还在 war 文件中构建了一个 Web 服务,当我测试 RESTful 服务 (http://localhost:8080/Test/Query?key=Hello%20World) 时,我得到了成功的响应。

我做错了什么,无法访问网页?

"Could not find resource for relative : /index.html" 表示您的应用正在尝试将 index.html 作为 RESTful 资源而不是静态文件进行挖掘。

没有看到您的 web.xml,我猜您有一个处理所有 URL 模式的 <servlet-mapping> 条目;像这样:

<servlet-mapping>
  <servlet-name>My RESTful servlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

为了解决这个问题,this SO 看起来很有帮助,它实际上建议在您的资源路径中添加一个额外的标记,例如:

<url-pattern>/service/*</url-pattern>

这意味着您必须以 http://localhost:8080/Test/service/Query?key=Hello%20World.

的身份访问您的查询资源