JSF tag is not working on *.xhtml but working *.jsp file

8.5k views Asked by At

I am trying to run simple application using JSF 2.2, Netbeans 7.3 and GlassFish v2.

index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
       <b>Hello from Facelets</b>
       <h:form id="this">
           <h:outputText value="This is"/>
       </h:form>
    </h:body>
</html>

web.xml:

<web-app version="2.5" 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_2_5.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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>  
</web-app>

Output:


Output file


The <b>Hello from Facelets</b> is working but <h:outputText value="this is "/> is not working. How is this caused and how can I solve it?

I searched here and found the following questions:

However, the answers did not solve myproblem.


Update: @Xtreme Biker, when I changed like you said I got following exception:

This is exception

Note: If I used *.jsp instead of *.xhtml it works. But when I make my index file extension xhtml it is not working.

3

There are 3 answers

0
BalusC On BEST ANSWER

You're using GlassFish v2, which is an ancient Java EE 5 container which already bundles JSF 1.2. The webapp-supplied JSF is by default ignored and essentially, you're running JSF 1.2 all the time. That explains why JSP works fine. Facelets is only supported since JSF 2.0.

You have the following options, depending on whether your requirement is being able to use JSF 2.2, or being restricted to GlassFish v2:

  1. If you're restricted to GlassFish v2, then you can't use JSF 2.2 at all. JSF 2.2 requires a minimum of Java EE 6 (GlassFish 3). You can however use JSF 2.0 or 2.1. You can download latest JSF 2.1 from here (currently, 2.1.25). Drop the javax.faces.jar in /WEB-INF/lib and edit the /WEB-INF/sun-web.xml to add the following entries to <sun-web-app>:

    <class-loader delegate="false"/> 
    <property name="useBundledJsf" value="true" />
    

    This will basically instruct GlassFish to prefer webapp-bundled JSF over its own bundled JSF.


  2. If you're not restricted to GlassFish v2 and can upgrade it, then do it as soon as possible. GlassFish v2 is an ancient container from May 2006 and succeeded by GlassFish 3 (Java EE 6) in Dec 2009 which is in turn succeeded by GlassFish 4 (Java EE 7) in May 2013.

    GlassFish 3.0 bundles JSF 2.0 and GlassFish 3.1 bundles JSF 2.1. But both are upgradeable to JSF 2.2 the same way as GlassFish v2 with the difference that sun-web.xml has been renamed to glassfish-web.xml. Another way is simply replacing the jsf-api.jar+jsf-impl.jar or javax.faces.jar in GlassFish's /modules directory by the desired version.

    GlassFish 4.0 bundles JSF 2.2 and you don't need to manually supply any JARs. I would currently however not recommend using GlassFish 4.0. It's too buggy (like as every first major release of GlassFish). Better wait for 4.0.1 (if it ever comes out) or a 4.1.


  3. If you're restricted to GlassFish v2 and can't upgrade to JSF 2.x somehow, but you really, really want to use Facelets, then you can always install Facelets 1.x separately. The procedure is described in this docbook. However, whilst you've got the advantages of using Facelets instead of JSP, sticking to JSF 1.x is disavantageous. I really wouldn't recommend that.

5
newuser On

Try to replace this,

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

instead of

 xmlns:h="http://xmlns.jcp.org/jsf/html"
9
Aritz On

Change your servlet mapping in order to work with .xhtml files. It seems not to be yet converting the tags.

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>