Integrating JSF 2.0 and Weld with Tomcat6


While Mojarra 2.0.0 is out on the street and Weld (aka Java Contexts and Dependency Injection (JSR-299)) is reaching CR1 there’s not much content on the web about it. So I tried to give a litte tutorial how they can be integrated with Tomcat6 (Glassfish v3 and JBoss provide built in support).What’s needed?

Thats’s not really much (just about 5 MB of JARs) but all you need to get your application running. I’m not going into detail on how to configurate JSF but just what’s additionally required for Weld.

First you have to “tell” Weld and Tomcat that Weld is present by including the Weld Servlet listener in your application’s web.xml:

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

After that create an empty file called “beans.xml” in the WEB-INF folder. Now Weld can do its magical stuff with all incoming Servlet request. Buth there’s more. This was just the setup of the Weld Servlet environment. Next you need to tell Weld what BeanManager should be used. The BeanManager is the reference to the Injection Manager itself.

The first thing to add is a resource reference in web.xml:

<resource-env-ref>
 <description>Object factory for the CDI Bean Manager</description>
 <resource-env-ref-name>BeanManager</resource-env-ref-name>
 <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>

And finally if not already present create META-INF/context.xml and put the Resource itself in it:

<Resource
 name="BeanManager"
 auth="Container"
 type="javax.enterprise.inject.spi.BeanManager"
 factory="org.jboss.weld.resources.ManagerObjectFactory"
/>

That’s all. Tomcat6 is now running with JSF 2.0 and Weld 1.0.0CR1 and ready for hours of fun. As you might have already guessed this post doesn’t contain anything about how to use Weld but just how to set it up with Tomcat6. However I’m writing a short introduction for Wled which should be online within 3 or 4 days.

UPDATE: For those using Maven here’s a simple POM containing the required repositories and dependencies:

<project
 xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>de.publicstaticfinal</groupId>
 <artifactId>WeldJSF2</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <repositories>
  <repository>
   <id>maven2-repository.dev.java.net</id>
   <name>Java.net Repository for Maven</name>
   <url>http://download.java.net/maven/2/</url>
   <layout>default</layout>
  </repository>
  <repository>
  <snapshots />
   <id>ibiblio</id>
   <url>http://www.ibiblio.org/maven2/</url>
   </repository>
   <repository>
   <id>JBoss</id>
   <url>http://repository.jboss.com/maven2</url>
  </repository>
 </repositories>
 <dependencies>
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-api</artifactId>
   <version>2.0.1</version>
  </dependency>
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-impl</artifactId>
   <scope>runtime</scope>
   <version>2.0.1</version>
  </dependency>
  <dependency>
   <groupId>javax.enterprise</groupId>
   <artifactId>cdi-api</artifactId>
   <scope>provided</scope>
   <version>1.0-CR1</version>
  </dependency>
  <dependency>
   <groupId>javax.annotation</groupId>
   <artifactId>jsr250-api</artifactId>
   <version>1.0</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.jboss.weld.servlet</groupId>
   <artifactId>weld-servlet</artifactId>
   <version>1.0.0-CR1</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.glassfish.web</groupId>
   <artifactId>el-impl</artifactId>
   <version>2.1.2-b04</version>
   <scope>runtime</scope>
   <exclusions>
    <exclusion>
     <groupId>javax.el</groupId>
     <artifactId>el-api</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
 </dependencies>
</project>

, , ,

  1. #1 by tooreduew on November 3, 2009 - 4:31 am

    Other variant is possible also

  2. #2 by Primo05 on December 7, 2009 - 2:36 am

    I tried what is described in this post (actually this is same as in weld documentation, post just doesn’t mention element around in META-INF/context.xml) with Mojarra 2.0.1 and Weld 1.0.0-CR1 on Tomcat 6.

    Without Weld (just Mojarra 2.0.1) sample .xhtml JSF page displays fine, but with Weld, although it is well initialized (based on what is displayed in console), this happens upon page request:
    When Faces servlet is mapped to something other than *.xhtml, xhtml page is shown in browser without any processng, as text file.
    When faces servlet mapping is *.xhtml, an infinite funcion calls occur cousing stack overlow error.

    Here is where is the loop:
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:311)
    com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:542)
    com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:350)
    com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:129)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:311)
    com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:542)
    com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:350)
    com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:129)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:311)

  3. #3 by feroz on December 8, 2009 - 4:15 pm

    Hi,

    I am new to web beans(weld). can you please give step by step guide lines to integrate jsf2 api, weld and tomcat 6. I am using net beans IDE. shall i place all jars( jsf-api.jar, jsf-impl.jar, weld-servlet.jar, JSR-250.jar, jstl 1.2.jar and el-impl 2.1.2-b04.jar) in tomcat/lib folder?
    Thanks you very much.

  4. #4 by Daniel on December 9, 2009 - 1:54 pm

    Primo05 :

    I tried what is described in this post (actually this is same as in weld documentation, post just doesn’t mention element around in META-INF/context.xml) with Mojarra 2.0.1 and Weld 1.0.0-CR1 on Tomcat 6.

    Without Weld (just Mojarra 2.0.1) sample .xhtml JSF page displays fine, but with Weld, although it is well initialized (based on what is displayed in console), this happens upon page request:
    When Faces servlet is mapped to something other than *.xhtml, xhtml page is shown in browser without any processng, as text file.
    When faces servlet mapping is *.xhtml, an infinite funcion calls occur cousing stack overlow error.

    First of all, you should switch to Weld-1.0.0 since some issue have been fixed there. Maybe this is related to it.
    If your problem still exist, post a message at the Weld user forum. There will be help:
    http://www.seamframework.org/Community/WeldUsers

  5. #5 by Daniel on December 9, 2009 - 3:49 pm

    feroz :

    Hi,

    I am new to web beans(weld). can you please give step by step guide lines to integrate jsf2 api, weld and tomcat 6. I am using net beans IDE. shall i place all jars( jsf-api.jar, jsf-impl.jar, weld-servlet.jar, JSR-250.jar, jstl 1.2.jar and el-impl 2.1.2-b04.jar) in tomcat/lib folder?
    Thanks you very much.

    Well. It’s up to you, where to place the JARs. I would recommend that you put them into the WEB-INF/lib folder of your web application.

  6. #6 by Feroz on December 9, 2009 - 3:56 pm

    Daniel :

    feroz :
    Hi,
    I am new to web beans(weld). can you please give step by step guide lines to integrate jsf2 api, weld and tomcat 6. I am using net beans IDE. shall i place all jars( jsf-api.jar, jsf-impl.jar, weld-servlet.jar, JSR-250.jar, jstl 1.2.jar and el-impl 2.1.2-b04.jar) in tomcat/lib folder?
    Thanks you very much.

    Well. It’s up to you, where to place the JARs. I would recommend that you put them into the WEB-INF/lib folder of your web application.

    I figured it out. I put the jars in application library and its working now. Thank you very much.

  7. #7 by Dharminder on July 12, 2010 - 10:04 pm

    Hi,

    I tried integrating with tomcat 6.0 and had problems because it seems as though Weld requires servlet 3.0. Is this true?

    dharminder

  8. #8 by Flavio Almeida on March 25, 2011 - 12:12 pm

    Manual lookup for BeanManager in Tomcat 7 returns null allways. Any advice?

(will not be published)

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 1,915 bad guys.