JSF2.0不呈现任何页面

JSF2.0不呈现任何页面,jsf,jakarta-ee,jsf-2,glassfish-3,web.xml,Jsf,Jakarta Ee,Jsf 2,Glassfish 3,Web.xml,我是一个刚开始使用JavaEE的程序员,在设置我的第一个支持“JSF2.0”的应用程序时遇到了麻烦。我正在使用以下技术: jdk1.6.0_24 MyEclipse 10.0 玻璃鱼3.0.1 JSF2.0 问题:我找不到让服务器解析和呈现JSF2.0页面的方法。我遵循了各种指南,尤其是stackoverflow.com的指南,但没有成功。我的测试页面不是由Glassfish 3.0.1呈现的。我尝试了不同的方法来修复我的代码,包括: 1->web.xml 1.1->编辑的标题指定JFS 2.0

我是一个刚开始使用JavaEE的程序员,在设置我的第一个支持“JSF2.0”的应用程序时遇到了麻烦。我正在使用以下技术:

jdk1.6.0_24 MyEclipse 10.0 玻璃鱼3.0.1 JSF2.0 问题:我找不到让服务器解析和呈现JSF2.0页面的方法。我遵循了各种指南,尤其是stackoverflow.com的指南,但没有成功。我的测试页面不是由Glassfish 3.0.1呈现的。我尝试了不同的方法来修复我的代码,包括:

1->web.xml

1.1->编辑的标题指定JFS 2.0 HTML标记库属性

<?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">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:f="http://java.sun.com/jsf/core">
1.2–定义的JSFServlet和其他内容

   <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
  </context-param>

  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
 </context-param>

 <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</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>/register.xhtml</url-pattern>
  </servlet-mapping>
2->多次验证以检查所使用的请求URL是否与FacesServlet的URL模式匹配

  **Target page** (url was not typed in browser . . . ): register.xhtml

  **URL Pattern:** 

  <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>/register.xhtml</url-pattern>
  </servlet-mapping>
3->主页

3.1.1->包含指定JFS 2.0 HTML标记库属性的标题

<?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">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:f="http://java.sun.com/jsf/core">
4–库和依赖项

用Maven 2.0支持开始我的项目。这是我的pom.xml文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <modelVersion>4.0.0</modelVersion>
  <groupId>JSF2.0_Template_1</groupId>
  <artifactId>JSF2.0_Template_1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name></name>
  <description></description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>


<dependency>
  <groupId>com.sun.el</groupId>
  <artifactId>el-ri</artifactId>
  <version>1.0</version>
</dependency>

<dependency>    
  <groupId>org.glassfish</groupId>
  <artifactId>bean-validator</artifactId>
  <version>3.0-JBoss-4.0.2</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.annotation</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.ejb</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.enterprise.deploy</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.jms</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.management.j2ee</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>javax.persistence</artifactId>
  <version>2.0.0</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.resource</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.security.auth.message</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.security.jacc</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.servlet</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.servlet.jsp</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.servlet.jsp.jstl</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.transaction</artifactId>
  <version>3.0.1</version>
</dependency>

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api-osgi</artifactId>
  <version>2.2.1</version>
</dependency>

<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>jsr311-api</artifactId>
  <version>1.1.1</version>
</dependency>

<dependency>
  <groupId>org.glassfish.web</groupId>
  <artifactId>jstl-impl</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.3</version>
</dependency>

<dependency>
  <groupId>javax.xml</groupId>
  <artifactId>webservices-api-osgi</artifactId>
  <version>2.0.1</version>
</dependency>

<dependency>
  <groupId>org.jboss.weld</groupId>
  <artifactId>weld-osgi-bundle</artifactId>
  <version>1.0.1-SP3</version>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.0.3</version>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.3</version>
</dependency>

  </dependencies>


  <build>
    <plugins>

  <plugin>
    <artifactId>maven-war-plugin</artifactId>
  </plugin>

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>

</plugins>

  </build>
</project>
5–Glassfish结果

5.1-HTML JSF 2.0代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">

<h:head>
  <title>A Simple JavaServer Faces Registration Application</title>
</h:head>
<h:body>
  <h:form>
    <h2>JSF Registration App</h2>
    <h4>Registration Form</h4>
    <table>
      <tr>
        <td>First Name:</td>
        <td>
          <h:inputText label="First Name" 
                   id="fname" value="#{userBean.firstName}" 
                       required="true"/>
          <h:message for="fname" />
        </td>
      </tr>
      <tr>
        <td>Last Name:</td>
        <td>
      <h:inputText label="Last Name"
                   id="lname" value="#{userBean.lastName}"
                   required="true"/>
          <h:message for="lname" />
        </td>
      </tr>
      <tr>
        <td>Sex:</td>
        <td>
          <h:selectOneRadio label="Sex" 
                            id="sex" value="#{userBean.sex}" required="true">
            <f:selectItem itemLabel="Male" itemValue="male" />
            <f:selectItem itemLabel="Female" itemValue="female" />
          </h:selectOneRadio>
          <h:message for="sex" />
        </td>
      </tr>
      <tr>
        <td>Date of Birth:</td>
        <td>
          <h:inputText label="Date of Birth"
                   id="dob" value="#{userBean.dob}" required="true">
            <f:convertDateTime pattern="MM-dd-yy" />
          </h:inputText> (mm-dd-yy)
          <h:message for="dob" />
        </td>
      </tr>
      <tr>
        <td>Email Address:</td>
        <td>
          <h:inputText label="Email Address"
                   id="email" value="#{userBean.email}" required="true"
                   validator="#{userBean.validateEmail}"/>
          <h:message for="email" />
        </td>
      </tr>
      <tr>
        <td>Service Level:</td>
        <td>
          <h:selectOneMenu label="Service Level"
                       value="#{userBean.serviceLevel}">
            <f:selectItem itemLabel="Medium" itemValue="medium" />
            <f:selectItem itemLabel="Basic" itemValue="basic" />
            <f:selectItem itemLabel="Premium" itemValue="premium" />
          </h:selectOneMenu>
        </td>
      </tr>
    </table>
    <p><h:commandButton value="Register" action="confirm" /></p>
  </h:form>
</h:body>
</html>
5.2-浏览器显示的页面的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
  <title>A Simple JavaServer Faces Registration Application</title></head><body>
<form id="j_idt6" name="j_idt6" method="post" action="/JSF2.0_Template/faces/register.xhtml;jsessionid=db8f214e61a90013726b072f83eb" enctype="application/x-www-form-    urlencoded">
<input type="hidden" name="j_idt6" value="j_idt6" />

    <h2>JSF Registration App</h2>
    <h4>Registration Form</h4>
    <table>
      <tr>
        <td>First Name:</td>
        <td><input id="j_idt6:fname" type="text" name="j_idt6:fname" />
        </td>
      </tr>
          <tr>
        <td>Last Name:</td>
        <td><input id="j_idt6:lname" type="text" name="j_idt6:lname" />
        </td>
      </tr>
      <tr>
        <td>Sex:</td>
        <td><table id="j_idt6:sex">
    <tr>
<td>
<input type="radio" name="j_idt6:sex" id="j_idt6:sex:0" value="male" /><label     for="j_idt6:sex:0"> Male</label></td>
<td>
<input type="radio" name="j_idt6:sex" id="j_idt6:sex:1" value="female" /><label     for="j_idt6:sex:1"> Female</label></td>
    </tr>
</table>
        </td>
      </tr>
      <tr>
        <td>Date of Birth:</td>
        <td><input id="j_idt6:dob" type="text" name="j_idt6:dob" /> (mm-dd-yy)

        </td>
  </tr>
          <tr>
        <td>Email Address:</td>
        <td><input id="j_idt6:email" type="text" name="j_idt6:email" />
        </td>
      </tr>
      <tr>
        <td>Service Level:</td>
        <td><select name="j_idt6:j_idt21" size="1"> <option value="medium"     selected="selected">Medium</option>
    <option value="basic">Basic</option>
    <option value="premium">Premium</option>
</select>
        </td>
      </tr>
    </table>
    <p><input type="submit" name="j_idt6:j_idt26" value="Register" /></p><input         type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState"         value="-3247001945221977595:-4814431455900353505" autocomplete="off" />
</form></body>
</html>
我无法上传浏览器显示内容的打印屏幕,但标记未呈现

问题:我犯了什么错误?要使Glassfish应用服务器正确呈现支持JSF2.0的页面,应该做些什么?我只需要一个简单的JSF2.0模板来设置我的开发环境,并将其用作JSF应用程序的起点


谢谢

与您所说的相反,JSF页面被正确呈现。根据获得的HTTP响应,生成的HTML输出看起来很好。所有JSF标记都被正确解析和呈现。如果JSF没有呈现任何内容,那么您将在源代码视图中看到原始JSF源代码

您的具体问题是浏览器没有正确显示生成的HTML。当内容类型标题错误时,可能会发生这种情况。内容类型标题告诉浏览器如何解释内容。这种行为也依赖于浏览器。例如,如果您向MSIE浏览器提供内容类型为application/xhtml+xml的服务,那么它将受到严重破坏,甚至会弹出令人困惑的另存为对话框

在您的情况下,即使您没有清楚地说明问题,也没有详细描述真实的响应,即使在不同的主机上也没有显示屏幕截图,但我认为您的具体问题是,所有HTML都是以文本/纯文本的内容类型显示的。也就是说,您实际上将整个HTML源代码视为浏览器响应,并将其误解为JSF没有呈现响应,而实际上是您的浏览器没有将检索到的JSF生成的HTML源呈现为可视化表示

正确的内容类型在默认情况下由JSF实现本身设置,但它可以被服务器覆盖,甚至可以被前面的ApacheHTTPD这样的代理覆盖,或者在JSF源代码中显式指定它。我不确定在您的特定情况下的确切原因,但是下面应该强制JSF以text/html的形式显式发送响应

您可以在任何现代web浏览器调试工具中检查响应头。例如,在Firebug中:


与具体问题无关,您的FacesServlet映射应该比此页面更通用,否则在提供Ajax Java脚本等资源时可能会遇到麻烦。我推荐*.xhtml。我还建议根据更合适的教程重新进行配置。您可以在本教程的底部找到好的教程。

与您所说的相反,JSF页面的呈现是正确的。根据获得的HTTP响应,生成的HTML输出看起来很好。所有JSF标记都被正确解析和呈现。如果JSF没有呈现任何内容,那么您将在源代码视图中看到原始JSF源代码

您的具体问题是浏览器没有正确显示生成的HTML。当内容类型标题错误时,可能会发生这种情况。内容类型标题告诉浏览器如何解释内容。这种行为也依赖于浏览器。例如,如果您向MSIE浏览器提供内容类型为application/xhtml+xml的服务,那么它将受到严重破坏,甚至会弹出令人困惑的另存为对话框

在您的情况下,即使您没有清楚地说明问题,也没有详细描述真实的响应,即使在不同的主机上也没有显示屏幕截图,但我认为您的具体问题是,所有HTML都是以文本/纯文本的内容类型显示的。也就是说,您实际上将整个HTML源代码视为浏览器响应,并将其误解为JSF没有呈现响应,而实际上是您的浏览器没有将检索到的JSF生成的HTML源呈现为可视化表示

默认情况下,正确的内容类型由JSF实现本身设置,但它可以被服务器覆盖,甚至可以被前端的代理(如ApacheHTTPD)覆盖,也可以被显式覆盖 在JSF源代码中指定它。我不确定在您的特定情况下的确切原因,但是下面应该强制JSF以text/html的形式显式发送响应

您可以在任何现代web浏览器调试工具中检查响应头。例如,在Firebug中:

与具体问题无关,您的FacesServlet映射应该比此页面更通用,否则在提供Ajax Java脚本等资源时可能会遇到麻烦。我推荐*.xhtml。我还建议根据更合适的教程重新进行配置。你可以在我们网站的底部找到好的教程