Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

jakarta ee - JSF 2.0 not rendering any page

I’m a beginning Java EE programmer having troubles setting up my first “JSF 2.0” enabled application. I’m using the following technologies:

  • jdk1.6.0_24
  • MyEclipse 10.0
  • Glassfish 3.0.1
  • JSF 2.0

Problem: I couldn’t find a way to make my server parse and render JSF 2.0 pages. I followed various guides, (notably from stackoverflow.com) without success. My test pages are not rendered by Glassfish 3.0.1. I tried different approaches to fix my code including:

1 -> web.xml

1.1 -> edited header specifying JFS 2.0 HTML taglib attributes ( ..)

<?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">

1.2 – Defined JSF servlet and other stuff

   <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 -> Verified many times to check if the request URL used matches the url-pattern of the FacesServlet.

  **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 -> home page

3.1.1 -> included header specifying JFS 2.0 HTML taglib attributes ( ..)

<!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 – libraries & dependencies

Started my project with Maven 2.0 support . This is my pom.xml file:

<?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 results

5.1 - HTML JSF 2.0 Code:

<!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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In contrary to what you're telling, the JSF page is correctly rendered. As per the obtained HTTP response, the generated HTML output looks all fine. All JSF tags are correctly parsed and rendered. If JSF did not render anything, then you would have seen exactly the original JSF source code in View Source.

Your concrete problem is that the browser is not displaying the generated HTML properly. This can happen when the Content-Type header is wrong. The Content-Type header tells the browser how to interpret the content. The behaviour is also browser dependent. If you for example serve a <DOCTYPE html ...> with a Content-Type of application/xhtml+xml to the MSIE browser, then it will go havoc and even pop a confusing Save As dialogue.

In your case, even though you didn't state the problem clearly, nor described the real response in detail, nor has shown a screenshot (even though on a different host), I think that your concrete problem is that all that HTML is displayed with a Content-Type of text/plain. I.e. you're literally seeing the entire HTML source code as browser response and you misinterpreted it as "JSF not rendering the response" while it's actually your browser who didn't render the retrieved JSF-generated HTML source into a visual representation.

The right Content-Type is by default set by the JSF implementation itself, but it can be overridden by the server or even a proxy (like Apache HTTPD) in the front, or by explicitly specifying it in the JSF source code. I'm not sure about the exact cause in your particular case, but the following should force JSF to send the response explicitly as text/html.

<f:view contentType="text/html">
    Put at least h:body here.
</f:view>

You can examine the response headers in any modern web browser debugging tool. For example, in Firebug:

enter image description here


Unrelated to the concrete problem, your FacesServlet mapping should be more generic than alone this page, or you may run into trouble when serving resources like Ajax javascripts. I recommend *.xhtml. I also recommend to redo the configuration based on a more decent tutorial. You can find good tutorials in the bottom of our JSF wiki page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...