3 Replies Latest reply: Oct 3, 2010 6:02 PM by EJP RSS

    JSF 2 with Tomcat 6

    802518
      Has anyone tried running JSF2 on Tomcat 6? I tried to do this but running into EL compatibility issues. Tried various combinations but nothing worked. I did the setup and test based on the following post.

      http://weblogs.java.net/blog/cayhorstmann/archive/2009/12/29/jsf-20-and-tomcat

      If anyone accomplished this, please post the following

      1) list of libraries in tomcat lib
      2) list of libraries in WEB-INF/lib
      3) web.xml configuration

      I have the following config and my sample page only displays plain HTML but not any JSF text box fields.

      I added the following to tomcat lib

      el-api-2.2.jar
      el-impl-2.2.jar
      jstl-api-1.2.jar
      jstl-impl-1.2.jar

      I have the following in WEB-INF/lib

      jsf-api.jar
      jsf-impl.jar
      jstl-api-1.2.jar
      jstl-impl-1.2.jar
      weld-servlet.jar

      My web.xml is:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                id="WebApp_ID"
                version="2.5">
      <display-name>JsfTest</display-name>

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

      <listener>
      <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
      </listener>

      <servlet>
           <servlet-name>Faces Servlet</servlet-name>
           <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      </servlet>
      <servlet-mapping>
           <servlet-name>Faces Servlet</servlet-name>
           <url-pattern>*.faces</url-pattern>
      </servlet-mapping>

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

      </web-app>

      I also have an empty beans.xml in my WEB-INF folder. This is required for Weld.

      Anyone, plese help.
        • 1. Re: JSF 2 with Tomcat 6
          800186
          user1093102 wrote:
          Has anyone tried running JSF2 on Tomcat 6?
          Yep, I use the Mojarra JSF 2 implementation in Tomcat 6 all the time, and many of my students and clients have as well.
          I have used various different Mojarra 2.0.x releases without problems on Tomcat 6, Tomcat 7, Glassfish 3, and JBoss 6
          (minus the JAR files in the Java EE 6 containers). JSF 2 is supposed to run in any container that supports servlets 2.5 and later,
          and I have gotten reports of it running on the Google Cloud Server, which I think is running Jetty.

          Maybe you could be more specific about the constructs that caused problems and the error messages that you got?

          Here are some details:

          1) Here is my web.xml. The PROJECT_STAGE and welcome-file-list entries are optional. Your sample app had some
          extra things in the web.xml that were not related to JSF; I would suggest removing them first to test a barebones
          JSF 2 app before trying to combine technologies.
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            version="2.5">
            <servlet>
              <servlet-name>Faces Servlet</servlet-name>
              <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            </servlet>
            <servlet-mapping>
              <servlet-name>Faces Servlet</servlet-name>
              <url-pattern>*.jsf</url-pattern>
            </servlet-mapping>
            <context-param>
              <param-name>javax.faces.PROJECT_STAGE</param-name>
              <param-value>Development</param-value>
            </context-param>
            <welcome-file-list>
              <welcome-file>index.jsp</welcome-file>
              <welcome-file>index.html</welcome-file>
            </welcome-file-list>
          </web-app>
          2) The JAR files I have in WEB-INF/lib are jsf-api.jar, jsf-impl.jar, jstl-api-1.2.jar, and jstl-impl-1.2.jar.
          The JSTL JAR files are actually not needed for the simple apps I link to below, but are needed for
          any JSF 2.0 app that uses ui:repeat, so IMHO should be considered a required part of a JSF 2.0 app.

          3) Here is my faces-config.xml file (the trivial apps below don't use any declarations in it,
          so it just has start and end tags):
          <?xml version="1.0"?>
          <faces-config 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-facesconfig_2_0.xsd"
             version="2.0">
          
          </faces-config>
          4) Of course, it is not used by the server at all, but I also think it is a good practice to add the JSF 2.0
          project facet if you are using Eclipse 3.6. That way, you get some help from the IDE when editing faces-config.xml and any .xhtml file.

          5) Here are two very simple Eclipse projects that you can download and try. They follow the structure outlined above, and they work
          for me in Tomcat 6, Tomcat 7, and Glassfish 3 as is. They will run in JBoss 6 if you remove the JAR files from WEB-INF/lib (and, removing
          the JAR files is really the right thing to do in Glassfish also, although they do in fact run as is).
          [url http://www.coreservlets.com/JSF-Tutorial/jsf2/#Getting-Started]Sample JSF 2 App for Testing.


          Cheers-
          - Marty
          • 2. Re: JSF 2 with Tomcat 6
            EJP
            Marty

            Thanks for this invaluable posting. I had independently and painfully come to the same conclusions:

            - web.xml version 2.5
            - faces-config.xml version 2.0
            - javax.faces.webapp.FacesServlet
            - Facelets 1.2
            - JSTL 1.2

            Because you're in Facelets 1.2, you can still use a com.sun.facelets.impl.ResourceResolver, especially one that extends com.sun.facelets.impl.DefaultResourceResolver. This has to be changed to extend javax.faces.view.facelets.ResourceResolver when you go to Facelets 2.
            • 3. Re: JSF 2 with Tomcat 6
              802518
              Marty

              Thanks a lot for the response and I got it to work using barebones JSF. However, I intend to use CDI and hence I added weld-servlet.jar to my WEB-INF/lib and the following entry to my web.xml

              <listener>
              <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
              </listener>

              I was able to get my index.xhtml page to display but when I click the Login button the Ajax message doesn't display. I don't see any errors in the logs. Here is my 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://java.sun.com/jsf/html"
                   xmlns:f="http://java.sun.com/jsf/core">
              <h:head>
                   <title>Welcome</title>
              </h:head>
              <h:body>
                   <h:form prependId="false">
                        <h3>Please enter your name and password.</h3>
                        <table>
                             <tr>
                                  <td>Name</td>
                                  <td><h:inputText id="name" value="#{user.name}"/></td>
                             </tr>
                             <tr>
                                  <td>Password</td>
                                  <td><h:inputSecret id="password" value="#{user.password}"/></td>
                             </tr>
                        </table>
                        <p>
                             <h:commandButton value="Login">
                                  <f:ajax execute="name password" render="out" />
                             </h:commandButton>
                        </p>
                        <h3><h:outputText id="out" value="#{user.greeting}"/></h3>
                   </h:form>
              </h:body>
              </html>

              Here is my UserBean:

              import java.io.Serializable;

              //import javax.faces.bean.ManagedBean;
              import javax.inject.Named;
              //import javax.faces.bean.SessionScoped;
              import javax.enterprise.context.SessionScoped;

              //@ManagedBean(name="user")
              @Named("user")
              @SessionScoped
              public class UserBean implements Serializable {

              private String name = "";
              private String password;

              public String getName() { return name; }
              public void setName(String newValue) { name = newValue; }

              public String getPassword() { return password; }
              public void setPassword(String newValue) { password = newValue; }

              public String getGreeting() {
              if (name.length() == 0) return "";
              else return "Welcome to JSF2 + Ajax, " + name + "!";
              }
              }

              If you tried using CDI with JSF2.0 on Tomcat6, let me know. If I try replacing the EL implementation in tomcat lib with el-api-2.2.jar and el-impl-2.2.jar, I run into EL incompatibility issues.

              Thanks a lot again for your post. It was very helpful.