Wednesday, September 4, 2013

Setting up Tomcat7 for JSF2.0 and CDI (Core Java Server Faces 3rd edition)

In the book  "Core Java Server Faces 3rd edition", we've option to run our first example either in any of the J2EE6 compatible App-servers like Glassfish/JBoss7 etc or Tomcat.

Moreover you can use JSF annotations (@ManagedBean) or CDI (@Named). I was planning to use CDI to work on the basic example for JSF and trying to set up Tomcat7 with the help of this post . Earlier I thought weld-servlet.jar would be the only Jar file needed to make this work, but that didn't work.

At some point while setting up the app, I was getting this error:

Sep 4, 2013 11:05:12 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.jboss.weld.environment.servlet.Listener
java.lang.NoClassDefFoundError: javax/enterprise/inject/spi/Extension
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
After some googling and inspecting available Jars, i downloaded Weld version 2.0.3 Final from Weld Distribution Download page  and copied following Jars in my application WEB-INF/lib folder 

cdi-api.jar
weld-api.jar
weld-core-impl.jar
weld-core-jsf.jar
weld-servlet.jar
weld-servlet-core.jar
weld-spi.jar 

Additionaly added jar for JSF2.0 (javax.faces-2.2.1.jar) also in WEB-INF/lib from http://javaserverfaces.java.net/download.html.

Finally things started working. I'm not attaching the source code used for running the application, any simple JSF application that needs CDI in Tomcat environment would need these changes and configuration.


References:
http://www.theserverside.com/tutorial/Working-with-CDI-and-JSF-20-on-Tomcat-7-Configuring-Weld

No comments:

Post a Comment