JSF Tip #67 - A little more JSF

In JSF Tip #66 I described what you need to do to get JSF running on a Servlet container. Of course this is not the end of the story. What if you need CDI? Well than you need just a little more. First start with the dependency.

            
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>2.3.5.Final</version>
        <scope>compile</scope>
    </dependency>
        

Then make sure to add a beans.xml to your WEB-INF directory. Like the one below.


    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
                http://xmlns.jcp.org/xml/ns/javaee 
                http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
           bean-discovery-mode="annotated">
    </beans>
        

If you are running this on Tomcat you will also have to add the following to your context.xml.


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

If you are running this on Tomcat you will also have to add the following to your web.xml


    <resource-env-ref>
        <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 voila you now have CDI with JSF on a servlet container!

Posted October 6, 2016

Up