Take the 2-minute tour ×

Am using struts2 , EJB 3.0 ... My requirement is to call the EJB layer from struts2 action class . I hope there are two ways in achieving this :

1.Using @EJB annotation in Action class
2.Using JNDI look up 

I tried both , but the problem with JNDI lookup is , eventhough am using correct naming , am getting NameNotFoundException . So the ultimately , my team moved to other method which is using @EJB annotation .

But when am using @EJB annotation am getting null out of it , I think its not injected :

am getting the NullPointerException

code :

    BeanLocal bean ;

Can any one suggest me what i have to do further ... Also if there is anylink in SOF , please do refer me as i found nothing related to this

share|improve this question
The CDI plugin is likely the "best" approach, but you can also do it manually, a la longsystemit.com/javablog/?p=40. –  Dave Newton Aug 29 '12 at 14:43

3 Answers 3

You can't use the traditional dependency injection in Struts 2 action classes because actions aren't managed. However there is a way to achieve this by using the CDI plugin or Guice. You can also use JNDI look up but the syntax depends on your server. Your best option is to check the documentation according to what you have (JBoss 7.1, Glassfish...)

share|improve this answer

I had this same problem and here is how I solved it.

As Shinosha said, the @EJB annotation will not work since the action classes are managed by the Struts container.

In order to use JNDI lookup, I had to make the bean @Remote and specify a mappedName. Then the code is as follows (it depends on the server your are using, in my case Weblogic):

Context ctx = new InitialContext();
MyBean bean= (MyBean) ctx.lookup("MyBeanMappedName#myapp.MyBean");

The lookup string should be the fully qualified name of the bean.

share|improve this answer

I wrote following interceptors to solve this issue. Have a look and share any feedback you may have :


share|improve this answer

Your Answer


