我正在通过一个简单的“Hello World”应用程序学习 Java EE,当我使用 index.html
运行该程序时,它运行良好。但是,当我使用 index.xhtml
运行相同的程序时,它会抛出 404 错误,指出“请求的资源不可用。”
我的文件夹结构如下:
当我使用 index.html
运行程序时,我使用以下 URL:http://localhost:8081/index.html ,页面显示 Hello World。当我使用 index.xhtml
运行时,我尝试了两者: http://localhost:8081/example2/index.xhtml和 http://localhost:8081/index.xhtml 。两者都会给出 404 错误。
我的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
我还尝试了多种浏览器(Chrome 和 Firefox)。知道为什么我在 XHTML 上收到 404 而在 HTML 上却没有收到 404 吗?
最佳答案
您可以使用以下代码在 web.xml 中配置主页:
<display-name>NameOfProject</display-name>
<!-- Configuration of your home page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
所以你可以同时使用: http://localhost:8081/NameOfProject/或者 http://localhost:8081/NameOfProject/index.xhtml
关于XHTML 上出现 Java EE 404 错误,但 HTML 上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030715/