为什么我的 <h:form> 不可见?

Why isn't my <h:form> visible?

我正在尝试在 jsf 上学习本教程:https://netbeans.org/kb/docs/web/jsf20-intro.html

在 "Wiring Managed Beans to Pages" 章节中,您必须将 HTML 表单元素切换为等效的 JSF HTML 表单组件。但是在我这样做之后(只是 commenting/uncommenting 已经存在的代码)它是不可见的。

我已将 xmlns:h="http://xmlns.jcp.org/jsf/html" 添加到 html 标签;我没有从 IDE 中得到任何错误;表单显示在检查元素中;我也试过 xmlns:h="http://java.sun.com/jsf/html",但还是不行。

这是我的页面:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    <!--<h:outputStylesheet name="css/stylesheet.css" />-->

    <title>Greeting</title>
</head>

<body>
    <div id="mainContainer">

        <div id="left" class="subContainer greyBox">

            <h4>Hi, my name is Duke!</h4>

            <h5>I'm thinking of a number

                <br/>
                between
                <span class="highlight">0</span> and
                <span class="highlight">10</span>.</h5>

            <h5>Can you guess it?</h5>

            <!-- <form action="response.xhtml">
                <input type="text" size="2" maxlength="2" />
                <input type="submit" value="submit" />
            </form>-->

            <h:form>
                <h:inputText id="userNumber"
                             size="2"
                             maxlength="2"
                             value="#{UserNumberBean.userNumber}"/>

                <h:commandButton id="submit" value="submit" action="response"/>
            </h:form>
        </div>

        <div id="right" class="subContainer">

            <img src="duke.png" alt="Duke waving" />
            <!--<h:graphicImage url="/duke.png" alt="Duke waving" />-->

        </div>
    </div>
</body>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

JSF 标签,如 <h:form><h:inputText> 被返回给客户端(您的浏览器)而不是正确呈现 HTML 是您的整个设置错误的明确标志. (浏览器会适当地忽略它不理解的任何标签,这就是为什么您看不到表单的任何内容的原因。)

如果您像这样映射 URL 模式:

<url-pattern>/faces/*</url-pattern>

然后您应该删除第二个欢迎文件条目

<welcome-file>index.xhtml</welcome-file>

因为这永远行不通,因为它与模式不匹配,所以整个请求不会由 FacesServlet 处理,在这种情况下,纯 .xhtml 文件按原样提供!

(它甚至在您所指的教程中明确表示,在“添加 JSF 2.x 对 Web 应用程序的支持”部分的末尾。 )