XHTML 上出现 Java EE 404 错误,但 HTML 上没有

标签 java jsf jakarta-ee cdi

我正在通过一个简单的“Hello World”应用程序学习 Java EE,当我使用 index.html 运行该程序时,它运行良好。但是,当我使用 index.xhtml 运行相同的程序时,它会抛出 404 错误,指出“请求的资源不可用。”

我的文件夹结构如下:

enter image description here

当我使用 index.html 运行程序时,我使用以下 URL:http://localhost:8081/index.html ,页面显示 Hello World。当我使用 index.xhtml 运行时,我尝试了两者: http://localhost:8081/example2/index.xhtmlhttp://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/

相关文章:

jsf - 如何跳过 p :ajax 中的验证阶段

java - 如何使用JSP/Servlet将文件上传到服务器?

java - 我使用哪种 JVM 架构?

java - 如何在 Java Swing 中将 String 转换为 TreeNode?

spring - 将 Spring bean 注入(inject) JSF ManagedBean 时出错

java - jsf-api 和 jsf-impl 有什么关系?

java - 跟踪 Java EE 中的用户 Activity

java - 如何检测鼠标点击二次曲线的 "stroke"部分?

java - 如何在java中做一个数字键盘

java - 如何在 Tomcat 服务器上使用 jfs 在 Java Web 应用程序中定义文件路径