JSF 1.X Backing Bean - EJB 3 Dependency Injection, (Glassfish) "Problem" and Solution

In case the dependency between a JSR 1.2 backing bean and an EJB 3 doesn't work:

//a backing bean

public class OrderView{

    @EJB private OrderGateway orderGateway;

    public int getNrOfOrders(){

return orderGateway.get... //NullPointerException here

}

You should check the version of the web.xml. Dependency Injection was introduced with Java EE 5 and so the 2.5 Version of the Servlet spec. The header of the web.xml deployment descriptor should look like: <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"> 

If you are using the 2.4 version: <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  Glassfish v2 will (correctly) ignore the @EJB annotation and you will get a NullPointerException at the first access. 

See also this post for more details about the EJB 3.1 / JSF 2 synergy.

Comments:

Cheers for that, I can't believe I have spent about a week trying to work out why I kept getting a NullPointerException at the point of calling my sessionbean method. I have tried several examples but no one has entioned the web.xml version (2.5) before, a quick change and hey presto it works!

Posted by keith on February 14, 2010 at 12:31 PM CET #

Dear Adam,
same problem here with GF 2.1.1 / JSF 1.2, and a web.xml pointing to web-app version "2.5".
Any further hint?

Posted by HGU on June 17, 2010 at 10:55 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License